]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013-2014 Uwe Hermann <uwe@hermann-uwe.de> | |
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 | ||
21 | #include <config.h> | |
22 | #include <check.h> | |
23 | #include <glib/gstdio.h> | |
24 | #include <libsigrok/libsigrok.h> | |
25 | #include "lib.h" | |
26 | ||
27 | #define BUFSIZE (1000 * 1000) | |
28 | ||
29 | enum { | |
30 | CHECK_ALL_LOW, | |
31 | CHECK_ALL_HIGH, | |
32 | CHECK_HELLO_WORLD, | |
33 | }; | |
34 | ||
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; | |
41 | ||
42 | static void check_all_low(const struct sr_datafeed_logic *logic) | |
43 | { | |
44 | uint64_t i; | |
45 | uint8_t *data; | |
46 | ||
47 | for (i = 0; i < logic->length; i++) { | |
48 | data = logic->data; | |
49 | if (data[i * logic->unitsize] != 0) | |
50 | fail("Logic data was not all-0x00."); | |
51 | } | |
52 | } | |
53 | ||
54 | static void check_all_high(const struct sr_datafeed_logic *logic) | |
55 | { | |
56 | uint64_t i; | |
57 | uint8_t *data; | |
58 | ||
59 | for (i = 0; i < logic->length; i++) { | |
60 | data = logic->data; | |
61 | if (data[i * logic->unitsize] != 0xff) | |
62 | fail("Logic data was not all-0xff."); | |
63 | } | |
64 | } | |
65 | ||
66 | static void check_hello_world(const struct sr_datafeed_logic *logic) | |
67 | { | |
68 | uint64_t i; | |
69 | uint8_t *data, b; | |
70 | const char *h = "Hello world"; | |
71 | ||
72 | for (i = 0; i < logic->length; i++) { | |
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 | ||
80 | static 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 | ||
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); | |
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) | |
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); | |
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 | ||
184 | static void check_buf(GHashTable *options, const uint8_t *buf, int check, | |
185 | uint64_t samples, uint64_t *samplerate) | |
186 | { | |
187 | int ret; | |
188 | struct sr_input *in; | |
189 | const struct sr_input_module *imod; | |
190 | struct sr_session *session; | |
191 | struct sr_dev_inst *sdi; | |
192 | GString *gbuf; | |
193 | ||
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; | |
201 | ||
202 | gbuf = g_string_new_len((gchar *)buf, (gssize)samples); | |
203 | ||
204 | imod = sr_input_find("binary"); | |
205 | fail_unless(imod != NULL, "Failed to find input module."); | |
206 | ||
207 | in = sr_input_new(imod, options); | |
208 | fail_unless(in != NULL, "Failed to create input instance."); | |
209 | ||
210 | sdi = sr_input_dev_inst_get(in); | |
211 | ||
212 | sr_session_new(srtest_ctx, &session); | |
213 | sr_session_datafeed_callback_add(session, datafeed_in, NULL); | |
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); | |
218 | sr_input_free(in); | |
219 | ||
220 | sr_session_destroy(session); | |
221 | ||
222 | g_string_free(gbuf, TRUE); | |
223 | } | |
224 | ||
225 | START_TEST(test_input_binary_all_low) | |
226 | { | |
227 | uint64_t i, samplerate; | |
228 | GHashTable *options; | |
229 | uint8_t *buf; | |
230 | GVariant *gvar; | |
231 | ||
232 | buf = g_malloc0(BUFSIZE); | |
233 | ||
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); | |
240 | ||
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); | |
247 | } | |
248 | ||
249 | g_hash_table_destroy(options); | |
250 | g_free(buf); | |
251 | } | |
252 | END_TEST | |
253 | ||
254 | START_TEST(test_input_binary_all_high) | |
255 | { | |
256 | uint64_t i; | |
257 | uint8_t *buf; | |
258 | ||
259 | buf = g_malloc(BUFSIZE); | |
260 | memset(buf, 0xff, BUFSIZE); | |
261 | ||
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); | |
265 | ||
266 | g_free(buf); | |
267 | } | |
268 | END_TEST | |
269 | ||
270 | START_TEST(test_input_binary_all_high_loop) | |
271 | { | |
272 | uint8_t *buf; | |
273 | uint64_t bufsize; | |
274 | ||
275 | /* Note: _i is the loop variable from tcase_add_loop_test(). */ | |
276 | ||
277 | bufsize = (_i * 10); | |
278 | buf = g_malloc(BUFSIZE); | |
279 | memset(buf, 0xff, BUFSIZE); | |
280 | ||
281 | check_buf(NULL, buf, CHECK_ALL_HIGH, bufsize, NULL); | |
282 | ||
283 | g_free(buf); | |
284 | } | |
285 | END_TEST | |
286 | ||
287 | START_TEST(test_input_binary_hello_world) | |
288 | { | |
289 | uint64_t samplerate; | |
290 | uint8_t *buf; | |
291 | GHashTable *options; | |
292 | GVariant *gvar; | |
293 | ||
294 | buf = (uint8_t *)g_strdup("Hello world"); | |
295 | ||
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); | |
302 | ||
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); | |
306 | ||
307 | g_hash_table_destroy(options); | |
308 | g_free(buf); | |
309 | } | |
310 | END_TEST | |
311 | ||
312 | Suite *suite_input_binary(void) | |
313 | { | |
314 | Suite *s; | |
315 | TCase *tc; | |
316 | ||
317 | s = suite_create("input-binary"); | |
318 | ||
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); | |
326 | ||
327 | return s; | |
328 | } |