]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 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 <check.h> | |
22 | #include <glib/gstdio.h> | |
23 | #include "../include/libsigrok/libsigrok.h" | |
24 | #include "lib.h" | |
25 | ||
26 | #define FILENAME "foo.dat" | |
27 | #define MAX_FILESIZE (1 * 1000000) | |
28 | ||
29 | #define CHECK_ALL_LOW 0 | |
30 | #define CHECK_ALL_HIGH 1 | |
31 | #define CHECK_HELLO_WORLD 2 | |
32 | ||
33 | static uint64_t df_packet_counter = 0, sample_counter = 0; | |
34 | static gboolean have_seen_df_end = FALSE; | |
35 | static GArray *logic_channellist = NULL; | |
36 | static int check_to_perform; | |
37 | static uint64_t expected_samples; | |
38 | static uint64_t *expected_samplerate; | |
39 | ||
40 | static void check_all_low(const struct sr_datafeed_logic *logic) | |
41 | { | |
42 | uint64_t i; | |
43 | uint8_t *data; | |
44 | ||
45 | for (i = 0; i < logic->length; i++) { | |
46 | data = logic->data; | |
47 | if (data[i * logic->unitsize] != 0) | |
48 | fail("Logic data was not all-0x00."); | |
49 | } | |
50 | } | |
51 | ||
52 | static void check_all_high(const struct sr_datafeed_logic *logic) | |
53 | { | |
54 | uint64_t i; | |
55 | uint8_t *data; | |
56 | ||
57 | for (i = 0; i < logic->length; i++) { | |
58 | data = logic->data; | |
59 | if (data[i * logic->unitsize] != 0xff) | |
60 | fail("Logic data was not all-0xff."); | |
61 | } | |
62 | } | |
63 | ||
64 | static void check_hello_world(const struct sr_datafeed_logic *logic) | |
65 | { | |
66 | uint64_t i; | |
67 | uint8_t *data, b; | |
68 | const char *h = "Hello world"; | |
69 | ||
70 | for (i = 0; i < logic->length; i++) { | |
71 | data = logic->data; | |
72 | b = data[sample_counter + i]; | |
73 | if (b != h[sample_counter + i]) | |
74 | fail("Logic data was not 'Hello world'."); | |
75 | } | |
76 | } | |
77 | ||
78 | static void datafeed_in(const struct sr_dev_inst *sdi, | |
79 | const struct sr_datafeed_packet *packet, void *cb_data) | |
80 | { | |
81 | const struct sr_datafeed_meta *meta; | |
82 | const struct sr_datafeed_logic *logic; | |
83 | struct sr_config *src; | |
84 | uint64_t samplerate, sample_interval; | |
85 | GSList *l; | |
86 | const void *p; | |
87 | ||
88 | (void)cb_data; | |
89 | ||
90 | fail_unless(sdi != NULL); | |
91 | fail_unless(packet != NULL); | |
92 | ||
93 | if (df_packet_counter++ == 0) | |
94 | fail_unless(packet->type == SR_DF_HEADER, | |
95 | "The first packet must be an SR_DF_HEADER."); | |
96 | ||
97 | if (have_seen_df_end) | |
98 | fail("There must be no packets after an SR_DF_END, but we " | |
99 | "received a packet of type %d.", packet->type); | |
100 | ||
101 | p = packet->payload; | |
102 | ||
103 | switch (packet->type) { | |
104 | case SR_DF_HEADER: | |
105 | // g_debug("Received SR_DF_HEADER."); | |
106 | // fail_unless(p != NULL, "SR_DF_HEADER payload was NULL."); | |
107 | ||
108 | logic_channellist = srtest_get_enabled_logic_channels(sdi); | |
109 | fail_unless(logic_channellist != NULL); | |
110 | fail_unless(logic_channellist->len != 0); | |
111 | // g_debug("Enabled channels: %d.", logic_channellist->len); | |
112 | break; | |
113 | case SR_DF_META: | |
114 | // g_debug("Received SR_DF_META."); | |
115 | ||
116 | meta = packet->payload; | |
117 | fail_unless(p != NULL, "SR_DF_META payload was NULL."); | |
118 | ||
119 | for (l = meta->config; l; l = l->next) { | |
120 | src = l->data; | |
121 | // g_debug("Got meta key: %d.", src->key); | |
122 | switch (src->key) { | |
123 | case SR_CONF_SAMPLERATE: | |
124 | samplerate = g_variant_get_uint64(src->data); | |
125 | if (!expected_samplerate) | |
126 | break; | |
127 | fail_unless(samplerate == *expected_samplerate, | |
128 | "Expected samplerate=%" PRIu64 ", " | |
129 | "got %" PRIu64 "", samplerate, | |
130 | *expected_samplerate); | |
131 | // g_debug("samplerate = %" PRIu64 " Hz.", | |
132 | // samplerate); | |
133 | break; | |
134 | case SR_CONF_SAMPLE_INTERVAL: | |
135 | sample_interval = g_variant_get_uint64(src->data); | |
136 | (void)sample_interval; | |
137 | // g_debug("sample interval = %" PRIu64 " ms.", | |
138 | // sample_interval); | |
139 | break; | |
140 | default: | |
141 | /* Unknown metadata is not an error. */ | |
142 | g_debug("Got unknown meta key: %d.", src->key); | |
143 | break; | |
144 | } | |
145 | } | |
146 | break; | |
147 | case SR_DF_LOGIC: | |
148 | logic = packet->payload; | |
149 | fail_unless(p != NULL, "SR_DF_LOGIC payload was NULL."); | |
150 | ||
151 | // g_debug("Received SR_DF_LOGIC (%" PRIu64 " bytes, " | |
152 | // "unitsize %d).", logic->length, logic->unitsize); | |
153 | ||
154 | if (check_to_perform == CHECK_ALL_LOW) | |
155 | check_all_low(logic); | |
156 | else if (check_to_perform == CHECK_ALL_HIGH) | |
157 | check_all_high(logic); | |
158 | else if (check_to_perform == CHECK_HELLO_WORLD) | |
159 | check_hello_world(logic); | |
160 | ||
161 | sample_counter += logic->length / logic->unitsize; | |
162 | ||
163 | break; | |
164 | case SR_DF_END: | |
165 | // g_debug("Received SR_DF_END."); | |
166 | // fail_unless(p != NULL, "SR_DF_END payload was NULL."); | |
167 | have_seen_df_end = TRUE; | |
168 | if (sample_counter != expected_samples) | |
169 | fail("Expected %" PRIu64 " samples, got %" PRIu64 "", | |
170 | expected_samples, sample_counter); | |
171 | break; | |
172 | default: | |
173 | /* | |
174 | * Note: The binary input format doesn't support SR_DF_TRIGGER | |
175 | * and some other types, those should yield an error. | |
176 | */ | |
177 | fail("Invalid packet type: %d.", packet->type); | |
178 | break; | |
179 | } | |
180 | } | |
181 | ||
182 | static void check_buf(const char *filename, GHashTable *param, | |
183 | const uint8_t *buf, int check, uint64_t samples, | |
184 | uint64_t *samplerate) | |
185 | { | |
186 | int ret; | |
187 | struct sr_input *in; | |
188 | struct sr_input_format *in_format; | |
189 | struct sr_session *session; | |
190 | ||
191 | /* Initialize global variables for this run. */ | |
192 | df_packet_counter = sample_counter = 0; | |
193 | have_seen_df_end = FALSE; | |
194 | logic_channellist = NULL; | |
195 | check_to_perform = check; | |
196 | expected_samples = samples; | |
197 | expected_samplerate = samplerate; | |
198 | ||
199 | in_format = srtest_input_get("binary"); | |
200 | ||
201 | in = g_try_malloc0(sizeof(struct sr_input)); | |
202 | fail_unless(in != NULL); | |
203 | ||
204 | in->format = in_format; | |
205 | in->param = param; | |
206 | ||
207 | srtest_buf_to_file(filename, buf, samples); /* Create a file. */ | |
208 | ||
209 | ret = in->format->init(in, filename); | |
210 | fail_unless(ret == SR_OK, "Input format init error: %d", ret); | |
211 | ||
212 | sr_session_new(&session); | |
213 | sr_session_datafeed_callback_add(session, datafeed_in, NULL); | |
214 | sr_session_dev_add(session, in->sdi); | |
215 | in_format->loadfile(in, filename); | |
216 | sr_session_destroy(session); | |
217 | ||
218 | g_unlink(filename); /* Delete file again. */ | |
219 | } | |
220 | ||
221 | START_TEST(test_input_binary_all_low) | |
222 | { | |
223 | uint64_t i, samplerate; | |
224 | uint8_t *buf; | |
225 | GHashTable *param; | |
226 | ||
227 | buf = g_try_malloc0(MAX_FILESIZE); | |
228 | fail_unless(buf != NULL); | |
229 | ||
230 | param = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); | |
231 | fail_unless(param != NULL); | |
232 | g_hash_table_insert(param, g_strdup("samplerate"), g_strdup("1250")); | |
233 | samplerate = SR_HZ(1250); | |
234 | ||
235 | /* Check various filesizes, with/without specifying a samplerate. */ | |
236 | check_buf(FILENAME, NULL, buf, CHECK_ALL_LOW, 0, NULL); | |
237 | check_buf(FILENAME, param, buf, CHECK_ALL_LOW, 0, &samplerate); | |
238 | for (i = 1; i < MAX_FILESIZE; i *= 3) { | |
239 | check_buf(FILENAME, NULL, buf, CHECK_ALL_LOW, i, NULL); | |
240 | check_buf(FILENAME, param, buf, CHECK_ALL_LOW, i, &samplerate); | |
241 | ||
242 | } | |
243 | ||
244 | g_hash_table_destroy(param); | |
245 | g_free(buf); | |
246 | } | |
247 | END_TEST | |
248 | ||
249 | START_TEST(test_input_binary_all_high) | |
250 | { | |
251 | uint64_t i; | |
252 | uint8_t *buf; | |
253 | ||
254 | buf = g_try_malloc(MAX_FILESIZE); | |
255 | memset(buf, 0xff, MAX_FILESIZE); | |
256 | ||
257 | check_buf(FILENAME, NULL, buf, CHECK_ALL_LOW, 0, NULL); | |
258 | for (i = 1; i < MAX_FILESIZE; i *= 3) | |
259 | check_buf(FILENAME, NULL, buf, CHECK_ALL_HIGH, i, NULL); | |
260 | ||
261 | g_free(buf); | |
262 | } | |
263 | END_TEST | |
264 | ||
265 | START_TEST(test_input_binary_all_high_loop) | |
266 | { | |
267 | uint8_t *buf; | |
268 | ||
269 | /* Note: _i is the loop variable from tcase_add_loop_test(). */ | |
270 | ||
271 | buf = g_try_malloc((_i * 10) + 1); | |
272 | memset(buf, 0xff, _i * 10); | |
273 | ||
274 | check_buf(FILENAME, NULL, buf, CHECK_ALL_HIGH, _i * 10, NULL); | |
275 | ||
276 | g_free(buf); | |
277 | } | |
278 | END_TEST | |
279 | ||
280 | START_TEST(test_input_binary_hello_world) | |
281 | { | |
282 | uint64_t samplerate; | |
283 | uint8_t *buf; | |
284 | GHashTable *param; | |
285 | ||
286 | buf = (uint8_t *)g_strdup("Hello world"); | |
287 | ||
288 | param = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); | |
289 | fail_unless(param != NULL); | |
290 | g_hash_table_insert(param, g_strdup("samplerate"), g_strdup("1250")); | |
291 | samplerate = SR_HZ(1250); | |
292 | ||
293 | /* Check with and without specifying a samplerate. */ | |
294 | check_buf(FILENAME, NULL, buf, CHECK_HELLO_WORLD, 11, NULL); | |
295 | check_buf(FILENAME, param, buf, CHECK_HELLO_WORLD, 11, &samplerate); | |
296 | ||
297 | g_hash_table_destroy(param); | |
298 | g_free(buf); | |
299 | } | |
300 | END_TEST | |
301 | ||
302 | Suite *suite_input_binary(void) | |
303 | { | |
304 | Suite *s; | |
305 | TCase *tc; | |
306 | ||
307 | s = suite_create("input-binary"); | |
308 | ||
309 | tc = tcase_create("basic"); | |
310 | tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown); | |
311 | tcase_add_test(tc, test_input_binary_all_low); | |
312 | tcase_add_test(tc, test_input_binary_all_high); | |
313 | tcase_add_loop_test(tc, test_input_binary_all_high_loop, 0, 10); | |
314 | tcase_add_test(tc, test_input_binary_hello_world); | |
315 | suite_add_tcase(s, tc); | |
316 | ||
317 | return s; | |
318 | } |