Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libsigrok project. |
| 3 | * |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 4 | * Copyright (C) 2013-2014 Uwe Hermann <uwe@hermann-uwe.de> |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 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> |
Uwe Hermann | 6592c36 | 2014-07-20 12:03:29 +0200 | [diff] [blame] | 23 | #include "../include/libsigrok/libsigrok.h" |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 24 | #include "lib.h" |
| 25 | |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 26 | #define BUFSIZE 1000000 |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 27 | |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 28 | enum { |
| 29 | CHECK_ALL_LOW, |
| 30 | CHECK_ALL_HIGH, |
| 31 | CHECK_HELLO_WORLD, |
| 32 | }; |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 33 | |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 34 | static uint64_t df_packet_counter = 0, sample_counter = 0; |
| 35 | static gboolean have_seen_df_end = FALSE; |
Uwe Hermann | fca75cb | 2014-03-24 16:11:45 +0100 | [diff] [blame] | 36 | static GArray *logic_channellist = NULL; |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 37 | static int check_to_perform; |
| 38 | static uint64_t expected_samples; |
| 39 | static uint64_t *expected_samplerate; |
| 40 | |
Uwe Hermann | 25f94df | 2014-09-10 23:55:15 +0200 | [diff] [blame] | 41 | static void check_all_low(const struct sr_datafeed_logic *logic) |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 42 | { |
| 43 | uint64_t i; |
| 44 | uint8_t *data; |
| 45 | |
Uwe Hermann | 25f94df | 2014-09-10 23:55:15 +0200 | [diff] [blame] | 46 | for (i = 0; i < logic->length; i++) { |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 47 | data = logic->data; |
| 48 | if (data[i * logic->unitsize] != 0) |
| 49 | fail("Logic data was not all-0x00."); |
| 50 | } |
| 51 | } |
| 52 | |
Uwe Hermann | 25f94df | 2014-09-10 23:55:15 +0200 | [diff] [blame] | 53 | static void check_all_high(const struct sr_datafeed_logic *logic) |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 54 | { |
| 55 | uint64_t i; |
| 56 | uint8_t *data; |
| 57 | |
Uwe Hermann | 25f94df | 2014-09-10 23:55:15 +0200 | [diff] [blame] | 58 | for (i = 0; i < logic->length; i++) { |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 59 | data = logic->data; |
| 60 | if (data[i * logic->unitsize] != 0xff) |
| 61 | fail("Logic data was not all-0xff."); |
| 62 | } |
| 63 | } |
| 64 | |
Uwe Hermann | 25f94df | 2014-09-10 23:55:15 +0200 | [diff] [blame] | 65 | static void check_hello_world(const struct sr_datafeed_logic *logic) |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 66 | { |
| 67 | uint64_t i; |
| 68 | uint8_t *data, b; |
| 69 | const char *h = "Hello world"; |
| 70 | |
Uwe Hermann | 25f94df | 2014-09-10 23:55:15 +0200 | [diff] [blame] | 71 | for (i = 0; i < logic->length; i++) { |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 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 | |
| 79 | static 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 | |
Uwe Hermann | fca75cb | 2014-03-24 16:11:45 +0100 | [diff] [blame] | 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); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 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) |
Uwe Hermann | 25f94df | 2014-09-10 23:55:15 +0200 | [diff] [blame] | 156 | check_all_low(logic); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 157 | else if (check_to_perform == CHECK_ALL_HIGH) |
Uwe Hermann | 25f94df | 2014-09-10 23:55:15 +0200 | [diff] [blame] | 158 | check_all_high(logic); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 159 | else if (check_to_perform == CHECK_HELLO_WORLD) |
Uwe Hermann | 25f94df | 2014-09-10 23:55:15 +0200 | [diff] [blame] | 160 | check_hello_world(logic); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 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 | |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 183 | static void check_buf(GHashTable *options, const uint8_t *buf, int check, |
| 184 | uint64_t samples, uint64_t *samplerate) |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 185 | { |
| 186 | int ret; |
| 187 | struct sr_input *in; |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 188 | const struct sr_input_module *imod; |
Uwe Hermann | dfa4cd0 | 2014-07-20 12:09:10 +0200 | [diff] [blame] | 189 | struct sr_session *session; |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 190 | struct sr_dev_inst *sdi; |
| 191 | GString *gbuf; |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 192 | |
| 193 | /* Initialize global variables for this run. */ |
| 194 | df_packet_counter = sample_counter = 0; |
| 195 | have_seen_df_end = FALSE; |
Uwe Hermann | fca75cb | 2014-03-24 16:11:45 +0100 | [diff] [blame] | 196 | logic_channellist = NULL; |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 197 | check_to_perform = check; |
| 198 | expected_samples = samples; |
| 199 | expected_samplerate = samplerate; |
| 200 | |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 201 | gbuf = g_string_new_len((gchar *)buf, (gssize)samples); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 202 | |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 203 | imod = sr_input_find("binary"); |
| 204 | fail_unless(imod != NULL, "Failed to find input module."); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 205 | |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 206 | in = sr_input_new(imod, options); |
| 207 | fail_unless(in != NULL, "Failed to create input instance."); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 208 | |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 209 | sdi = sr_input_dev_inst_get(in); |
| 210 | fail_unless(sdi != NULL, "Failed to get device instance."); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 211 | |
Uwe Hermann | dfa4cd0 | 2014-07-20 12:09:10 +0200 | [diff] [blame] | 212 | sr_session_new(&session); |
| 213 | sr_session_datafeed_callback_add(session, datafeed_in, NULL); |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 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 | |
| 219 | ret = sr_input_free(in); |
| 220 | fail_unless(ret == SR_OK, "Failed to free input instance: %d", ret); |
| 221 | |
Uwe Hermann | dfa4cd0 | 2014-07-20 12:09:10 +0200 | [diff] [blame] | 222 | sr_session_destroy(session); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 223 | |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 224 | g_string_free(gbuf, TRUE); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | START_TEST(test_input_binary_all_low) |
| 228 | { |
| 229 | uint64_t i, samplerate; |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 230 | GHashTable *options; |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 231 | uint8_t *buf; |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 232 | GVariant *gvar; |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 233 | |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 234 | buf = g_malloc0(BUFSIZE); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 235 | |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 236 | gvar = g_variant_new_uint64(1250); |
| 237 | options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, |
| 238 | (GDestroyNotify)g_variant_unref); |
| 239 | g_hash_table_insert(options, g_strdup("samplerate"), |
| 240 | g_variant_ref_sink(gvar)); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 241 | samplerate = SR_HZ(1250); |
| 242 | |
| 243 | /* Check various filesizes, with/without specifying a samplerate. */ |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 244 | check_buf(NULL, buf, CHECK_ALL_LOW, 0, NULL); |
| 245 | check_buf(options, buf, CHECK_ALL_LOW, 0, &samplerate); |
| 246 | for (i = 1; i < BUFSIZE; i *= 3) { |
| 247 | check_buf(NULL, buf, CHECK_ALL_LOW, i, NULL); |
| 248 | check_buf(options, buf, CHECK_ALL_LOW, i, &samplerate); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 249 | } |
| 250 | |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 251 | g_hash_table_destroy(options); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 252 | g_free(buf); |
| 253 | } |
| 254 | END_TEST |
| 255 | |
| 256 | START_TEST(test_input_binary_all_high) |
| 257 | { |
| 258 | uint64_t i; |
| 259 | uint8_t *buf; |
| 260 | |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 261 | buf = g_malloc(BUFSIZE); |
| 262 | memset(buf, 0xff, BUFSIZE); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 263 | |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 264 | check_buf(NULL, buf, CHECK_ALL_HIGH, 0, NULL); |
| 265 | for (i = 1; i < BUFSIZE; i *= 3) |
| 266 | check_buf(NULL, buf, CHECK_ALL_HIGH, i, NULL); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 267 | |
| 268 | g_free(buf); |
| 269 | } |
| 270 | END_TEST |
| 271 | |
| 272 | START_TEST(test_input_binary_all_high_loop) |
| 273 | { |
| 274 | uint8_t *buf; |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 275 | uint64_t bufsize; |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 276 | |
| 277 | /* Note: _i is the loop variable from tcase_add_loop_test(). */ |
| 278 | |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 279 | bufsize = (_i * 10); |
| 280 | buf = g_malloc(BUFSIZE); |
| 281 | memset(buf, 0xff, BUFSIZE); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 282 | |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 283 | check_buf(NULL, buf, CHECK_ALL_HIGH, bufsize, NULL); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 284 | |
| 285 | g_free(buf); |
| 286 | } |
| 287 | END_TEST |
| 288 | |
| 289 | START_TEST(test_input_binary_hello_world) |
| 290 | { |
| 291 | uint64_t samplerate; |
| 292 | uint8_t *buf; |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 293 | GHashTable *options; |
| 294 | GVariant *gvar; |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 295 | |
| 296 | buf = (uint8_t *)g_strdup("Hello world"); |
| 297 | |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 298 | gvar = g_variant_new_uint64(1250); |
| 299 | options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, |
| 300 | (GDestroyNotify)g_variant_unref); |
| 301 | g_hash_table_insert(options, g_strdup("samplerate"), |
| 302 | g_variant_ref_sink(gvar)); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 303 | samplerate = SR_HZ(1250); |
| 304 | |
| 305 | /* Check with and without specifying a samplerate. */ |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 306 | check_buf(NULL, buf, CHECK_HELLO_WORLD, 11, NULL); |
| 307 | check_buf(options, buf, CHECK_HELLO_WORLD, 11, &samplerate); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 308 | |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 309 | g_hash_table_destroy(options); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 310 | g_free(buf); |
| 311 | } |
| 312 | END_TEST |
| 313 | |
| 314 | Suite *suite_input_binary(void) |
| 315 | { |
| 316 | Suite *s; |
| 317 | TCase *tc; |
| 318 | |
| 319 | s = suite_create("input-binary"); |
| 320 | |
| 321 | tc = tcase_create("basic"); |
Uwe Hermann | 98de0c7 | 2014-08-11 13:20:50 +0200 | [diff] [blame] | 322 | tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 323 | tcase_add_test(tc, test_input_binary_all_low); |
| 324 | tcase_add_test(tc, test_input_binary_all_high); |
Uwe Hermann | 95bc772 | 2014-08-29 21:32:34 +0200 | [diff] [blame] | 325 | tcase_add_loop_test(tc, test_input_binary_all_high_loop, 1, 10); |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 326 | tcase_add_test(tc, test_input_binary_hello_world); |
| 327 | suite_add_tcase(s, tc); |
| 328 | |
| 329 | return s; |
| 330 | } |