input_binary.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <check.h>
  21. #include <glib/gstdio.h>
  22. #include <libsigrok/libsigrok.h>
  23. #include "lib.h"
  24. #define BUFSIZE (1000 * 1000)
  25. enum {
  26. CHECK_ALL_LOW,
  27. CHECK_ALL_HIGH,
  28. CHECK_HELLO_WORLD,
  29. };
  30. static uint64_t df_packet_counter = 0, sample_counter = 0;
  31. static gboolean have_seen_df_end = FALSE;
  32. static GArray *logic_channellist = NULL;
  33. static int check_to_perform;
  34. static uint64_t expected_samples;
  35. static uint64_t *expected_samplerate;
  36. static void check_all_low(const struct sr_datafeed_logic *logic)
  37. {
  38. uint64_t i;
  39. uint8_t *data;
  40. for (i = 0; i < logic->length; i++) {
  41. data = logic->data;
  42. if (data[i * logic->unitsize] != 0)
  43. fail("Logic data was not all-0x00.");
  44. }
  45. }
  46. static void check_all_high(const struct sr_datafeed_logic *logic)
  47. {
  48. uint64_t i;
  49. uint8_t *data;
  50. for (i = 0; i < logic->length; i++) {
  51. data = logic->data;
  52. if (data[i * logic->unitsize] != 0xff)
  53. fail("Logic data was not all-0xff.");
  54. }
  55. }
  56. static void check_hello_world(const struct sr_datafeed_logic *logic)
  57. {
  58. uint64_t i;
  59. uint8_t *data, b;
  60. const char *h = "Hello world";
  61. for (i = 0; i < logic->length; i++) {
  62. data = logic->data;
  63. b = data[sample_counter + i];
  64. if (b != h[sample_counter + i])
  65. fail("Logic data was not 'Hello world'.");
  66. }
  67. }
  68. static void datafeed_in(const struct sr_dev_inst *sdi,
  69. const struct sr_datafeed_packet *packet, void *cb_data)
  70. {
  71. const struct sr_datafeed_meta *meta;
  72. const struct sr_datafeed_logic *logic;
  73. struct sr_config *src;
  74. uint64_t samplerate, sample_interval;
  75. GSList *l;
  76. const void *p;
  77. (void)cb_data;
  78. fail_unless(sdi != NULL);
  79. fail_unless(packet != NULL);
  80. if (df_packet_counter++ == 0)
  81. fail_unless(packet->type == SR_DF_HEADER,
  82. "The first packet must be an SR_DF_HEADER.");
  83. if (have_seen_df_end)
  84. fail("There must be no packets after an SR_DF_END, but we "
  85. "received a packet of type %d.", packet->type);
  86. p = packet->payload;
  87. switch (packet->type) {
  88. case SR_DF_HEADER:
  89. // g_debug("Received SR_DF_HEADER.");
  90. // fail_unless(p != NULL, "SR_DF_HEADER payload was NULL.");
  91. logic_channellist = srtest_get_enabled_logic_channels(sdi);
  92. fail_unless(logic_channellist != NULL);
  93. fail_unless(logic_channellist->len != 0);
  94. // g_debug("Enabled channels: %d.", logic_channellist->len);
  95. break;
  96. case SR_DF_META:
  97. // g_debug("Received SR_DF_META.");
  98. meta = packet->payload;
  99. fail_unless(p != NULL, "SR_DF_META payload was NULL.");
  100. for (l = meta->config; l; l = l->next) {
  101. src = l->data;
  102. // g_debug("Got meta key: %d.", src->key);
  103. switch (src->key) {
  104. case SR_CONF_SAMPLERATE:
  105. samplerate = g_variant_get_uint64(src->data);
  106. if (!expected_samplerate)
  107. break;
  108. fail_unless(samplerate == *expected_samplerate,
  109. "Expected samplerate=%" PRIu64 ", "
  110. "got %" PRIu64 "", samplerate,
  111. *expected_samplerate);
  112. // g_debug("samplerate = %" PRIu64 " Hz.",
  113. // samplerate);
  114. break;
  115. case SR_CONF_SAMPLE_INTERVAL:
  116. sample_interval = g_variant_get_uint64(src->data);
  117. (void)sample_interval;
  118. // g_debug("sample interval = %" PRIu64 " ms.",
  119. // sample_interval);
  120. break;
  121. default:
  122. /* Unknown metadata is not an error. */
  123. g_debug("Got unknown meta key: %d.", src->key);
  124. break;
  125. }
  126. }
  127. break;
  128. case SR_DF_LOGIC:
  129. logic = packet->payload;
  130. fail_unless(p != NULL, "SR_DF_LOGIC payload was NULL.");
  131. // g_debug("Received SR_DF_LOGIC (%" PRIu64 " bytes, "
  132. // "unitsize %d).", logic->length, logic->unitsize);
  133. if (check_to_perform == CHECK_ALL_LOW)
  134. check_all_low(logic);
  135. else if (check_to_perform == CHECK_ALL_HIGH)
  136. check_all_high(logic);
  137. else if (check_to_perform == CHECK_HELLO_WORLD)
  138. check_hello_world(logic);
  139. sample_counter += logic->length / logic->unitsize;
  140. break;
  141. case SR_DF_END:
  142. // g_debug("Received SR_DF_END.");
  143. // fail_unless(p != NULL, "SR_DF_END payload was NULL.");
  144. have_seen_df_end = TRUE;
  145. if (sample_counter != expected_samples)
  146. fail("Expected %" PRIu64 " samples, got %" PRIu64 "",
  147. expected_samples, sample_counter);
  148. break;
  149. default:
  150. /*
  151. * Note: The binary input format doesn't support SR_DF_TRIGGER
  152. * and some other types, those should yield an error.
  153. */
  154. fail("Invalid packet type: %d.", packet->type);
  155. break;
  156. }
  157. }
  158. static void check_buf(GHashTable *options, const uint8_t *buf, int check,
  159. uint64_t samples, uint64_t *samplerate)
  160. {
  161. int ret;
  162. struct sr_input *in;
  163. const struct sr_input_module *imod;
  164. struct sr_session *session;
  165. struct sr_dev_inst *sdi;
  166. GString *gbuf;
  167. /* Initialize global variables for this run. */
  168. df_packet_counter = sample_counter = 0;
  169. have_seen_df_end = FALSE;
  170. logic_channellist = NULL;
  171. check_to_perform = check;
  172. expected_samples = samples;
  173. expected_samplerate = samplerate;
  174. gbuf = g_string_new_len((gchar *)buf, (gssize)samples);
  175. imod = sr_input_find("binary");
  176. fail_unless(imod != NULL, "Failed to find input module.");
  177. in = sr_input_new(imod, options);
  178. fail_unless(in != NULL, "Failed to create input instance.");
  179. sdi = sr_input_dev_inst_get(in);
  180. sr_session_new(srtest_ctx, &session);
  181. sr_session_datafeed_callback_add(session, datafeed_in, NULL);
  182. sr_session_dev_add(session, sdi);
  183. ret = sr_input_send(in, gbuf);
  184. fail_unless(ret == SR_OK, "sr_input_send() error: %d", ret);
  185. sr_input_free(in);
  186. sr_session_destroy(session);
  187. g_string_free(gbuf, TRUE);
  188. }
  189. START_TEST(test_input_binary_all_low)
  190. {
  191. uint64_t i, samplerate;
  192. GHashTable *options;
  193. uint8_t *buf;
  194. GVariant *gvar;
  195. buf = g_malloc0(BUFSIZE);
  196. gvar = g_variant_new_uint64(1250);
  197. options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
  198. (GDestroyNotify)g_variant_unref);
  199. g_hash_table_insert(options, g_strdup("samplerate"),
  200. g_variant_ref_sink(gvar));
  201. samplerate = SR_HZ(1250);
  202. /* Check various filesizes, with/without specifying a samplerate. */
  203. check_buf(NULL, buf, CHECK_ALL_LOW, 0, NULL);
  204. check_buf(options, buf, CHECK_ALL_LOW, 0, &samplerate);
  205. for (i = 1; i < BUFSIZE; i *= 3) {
  206. check_buf(NULL, buf, CHECK_ALL_LOW, i, NULL);
  207. check_buf(options, buf, CHECK_ALL_LOW, i, &samplerate);
  208. }
  209. g_hash_table_destroy(options);
  210. g_free(buf);
  211. }
  212. END_TEST
  213. START_TEST(test_input_binary_all_high)
  214. {
  215. uint64_t i;
  216. uint8_t *buf;
  217. buf = g_malloc(BUFSIZE);
  218. memset(buf, 0xff, BUFSIZE);
  219. check_buf(NULL, buf, CHECK_ALL_HIGH, 0, NULL);
  220. for (i = 1; i < BUFSIZE; i *= 3)
  221. check_buf(NULL, buf, CHECK_ALL_HIGH, i, NULL);
  222. g_free(buf);
  223. }
  224. END_TEST
  225. START_TEST(test_input_binary_all_high_loop)
  226. {
  227. uint8_t *buf;
  228. uint64_t bufsize;
  229. /* Note: _i is the loop variable from tcase_add_loop_test(). */
  230. bufsize = (_i * 10);
  231. buf = g_malloc(BUFSIZE);
  232. memset(buf, 0xff, BUFSIZE);
  233. check_buf(NULL, buf, CHECK_ALL_HIGH, bufsize, NULL);
  234. g_free(buf);
  235. }
  236. END_TEST
  237. START_TEST(test_input_binary_hello_world)
  238. {
  239. uint64_t samplerate;
  240. uint8_t *buf;
  241. GHashTable *options;
  242. GVariant *gvar;
  243. buf = (uint8_t *)g_strdup("Hello world");
  244. gvar = g_variant_new_uint64(1250);
  245. options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
  246. (GDestroyNotify)g_variant_unref);
  247. g_hash_table_insert(options, g_strdup("samplerate"),
  248. g_variant_ref_sink(gvar));
  249. samplerate = SR_HZ(1250);
  250. /* Check with and without specifying a samplerate. */
  251. check_buf(NULL, buf, CHECK_HELLO_WORLD, 11, NULL);
  252. check_buf(options, buf, CHECK_HELLO_WORLD, 11, &samplerate);
  253. g_hash_table_destroy(options);
  254. g_free(buf);
  255. }
  256. END_TEST
  257. Suite *suite_input_binary(void)
  258. {
  259. Suite *s;
  260. TCase *tc;
  261. s = suite_create("input-binary");
  262. tc = tcase_create("basic");
  263. tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown);
  264. tcase_add_test(tc, test_input_binary_all_low);
  265. tcase_add_test(tc, test_input_binary_all_high);
  266. tcase_add_loop_test(tc, test_input_binary_all_high_loop, 1, 10);
  267. tcase_add_test(tc, test_input_binary_hello_world);
  268. suite_add_tcase(s, tc);
  269. return s;
  270. }