]> sigrok.org Git - libsigrok.git/blob - tests/input_binary.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / tests / input_binary.c
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
20 #include <config.h>
21 #include <check.h>
22 #include <glib/gstdio.h>
23 #include <libsigrok/libsigrok.h>
24 #include "lib.h"
25
26 #define BUFSIZE (1000 * 1000)
27
28 enum {
29         CHECK_ALL_LOW,
30         CHECK_ALL_HIGH,
31         CHECK_HELLO_WORLD,
32 };
33
34 static uint64_t df_packet_counter = 0, sample_counter = 0;
35 static gboolean have_seen_df_end = FALSE;
36 static GArray *logic_channellist = NULL;
37 static int check_to_perform;
38 static uint64_t expected_samples;
39 static uint64_t *expected_samplerate;
40
41 static void check_all_low(const struct sr_datafeed_logic *logic)
42 {
43         uint64_t i;
44         uint8_t *data;
45
46         for (i = 0; i < logic->length; i++) {
47                 data = logic->data;
48                 if (data[i * logic->unitsize] != 0)
49                         fail("Logic data was not all-0x00.");
50         }
51 }
52
53 static void check_all_high(const struct sr_datafeed_logic *logic)
54 {
55         uint64_t i;
56         uint8_t *data;
57
58         for (i = 0; i < logic->length; i++) {
59                 data = logic->data;
60                 if (data[i * logic->unitsize] != 0xff)
61                         fail("Logic data was not all-0xff.");
62         }
63 }
64
65 static void check_hello_world(const struct sr_datafeed_logic *logic)
66 {
67         uint64_t i;
68         uint8_t *data, b;
69         const char *h = "Hello world";
70
71         for (i = 0; i < logic->length; i++) {
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
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);
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)
156                         check_all_low(logic);
157                 else if (check_to_perform == CHECK_ALL_HIGH)
158                         check_all_high(logic);
159                 else if (check_to_perform == CHECK_HELLO_WORLD)
160                         check_hello_world(logic);
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
183 static void check_buf(GHashTable *options, const uint8_t *buf, int check,
184                 uint64_t samples, uint64_t *samplerate)
185 {
186         int ret;
187         struct sr_input *in;
188         const struct sr_input_module *imod;
189         struct sr_session *session;
190         struct sr_dev_inst *sdi;
191         GString *gbuf;
192
193         /* Initialize global variables for this run. */
194         df_packet_counter = sample_counter = 0;
195         have_seen_df_end = FALSE;
196         logic_channellist = NULL;
197         check_to_perform = check;
198         expected_samples = samples;
199         expected_samplerate = samplerate;
200
201         gbuf = g_string_new_len((gchar *)buf, (gssize)samples);
202
203         imod = sr_input_find("binary");
204         fail_unless(imod != NULL, "Failed to find input module.");
205
206         in = sr_input_new(imod, options);
207         fail_unless(in != NULL, "Failed to create input instance.");
208
209         sdi = sr_input_dev_inst_get(in);
210
211         sr_session_new(srtest_ctx, &session);
212         sr_session_datafeed_callback_add(session, datafeed_in, NULL);
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);
217         sr_input_free(in);
218
219         sr_session_destroy(session);
220
221         g_string_free(gbuf, TRUE);
222 }
223
224 START_TEST(test_input_binary_all_low)
225 {
226         uint64_t i, samplerate;
227         GHashTable *options;
228         uint8_t *buf;
229         GVariant *gvar;
230
231         buf = g_malloc0(BUFSIZE);
232
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));
238         samplerate = SR_HZ(1250);
239
240         /* Check various filesizes, with/without specifying a samplerate. */
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);
246         }
247
248         g_hash_table_destroy(options);
249         g_free(buf);
250 }
251 END_TEST
252
253 START_TEST(test_input_binary_all_high)
254 {
255         uint64_t i;
256         uint8_t *buf;
257
258         buf = g_malloc(BUFSIZE);
259         memset(buf, 0xff, BUFSIZE);
260
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);
264
265         g_free(buf);
266 }
267 END_TEST
268
269 START_TEST(test_input_binary_all_high_loop)
270 {
271         uint8_t *buf;
272         uint64_t bufsize;
273
274         /* Note: _i is the loop variable from tcase_add_loop_test(). */
275
276         bufsize = (_i * 10);
277         buf = g_malloc(BUFSIZE);
278         memset(buf, 0xff, BUFSIZE);
279
280         check_buf(NULL, buf, CHECK_ALL_HIGH, bufsize, NULL);
281
282         g_free(buf);
283 }
284 END_TEST
285
286 START_TEST(test_input_binary_hello_world)
287 {
288         uint64_t samplerate;
289         uint8_t *buf;
290         GHashTable *options;
291         GVariant *gvar;
292
293         buf = (uint8_t *)g_strdup("Hello world");
294
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));
300         samplerate = SR_HZ(1250);
301
302         /* Check with and without specifying a samplerate. */
303         check_buf(NULL, buf, CHECK_HELLO_WORLD, 11, NULL);
304         check_buf(options, buf, CHECK_HELLO_WORLD, 11, &samplerate);
305
306         g_hash_table_destroy(options);
307         g_free(buf);
308 }
309 END_TEST
310
311 Suite *suite_input_binary(void)
312 {
313         Suite *s;
314         TCase *tc;
315
316         s = suite_create("input-binary");
317
318         tc = tcase_create("basic");
319         tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown);
320         tcase_add_test(tc, test_input_binary_all_low);
321         tcase_add_test(tc, test_input_binary_all_high);
322         tcase_add_loop_test(tc, test_input_binary_all_high_loop, 1, 10);
323         tcase_add_test(tc, test_input_binary_hello_world);
324         suite_add_tcase(s, tc);
325
326         return s;
327 }