2 * This file is part of the libsigrok project.
4 * Copyright (C) 2018 Gerhard Sittig <gerhard.sittig@gmx.net>
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.
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.
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/>.
21 * See the LA1034 vendor's http://www.pctestinstruments.com/ website.
23 * The hardware comes with (Windows only) software which uses the .lpf
24 * ("LogicPort File") filename extension for project files, which hold
25 * both the configuration as well as sample data (up to 2K samples). In
26 * the absence of an attached logic analyzer, the software provides a
27 * demo mode which generates random input signals. The software installs
28 * example project files (with samples), too.
30 * The file format is "mostly text", is line oriented, though it uses
31 * funny DC1 separator characters as well as line continuation by means
32 * of a combination of DC1 and slashes. Fortunately the last text line
33 * is terminated by means of CRLF.
35 * The software is rather complex and has features which don't easily
36 * translate to sigrok semantics (like one signal being a member of
37 * multiple groups, display format specs for groups' values).
39 * This input module implementation supports the following features:
40 * - input format auto detection
41 * - sample period to sample rate conversion
42 * - wire names, acquisition filters ("enabled") and inversion flags
43 * - decompression (repetition counters for sample data)
44 * - strict '0' and '1' levels (as well as ignoring 'U' values)
45 * - signal names (user assigned names, "aliases" for "wires")
46 * - signal groups (no support for multiple assignments, no support for
47 * display format specs)
48 * - "logic" channels (mere bits, no support for analog channels, also
49 * nothing analog "gets derived from" any signal groups) -- libsigrok
50 * using applications might provide such a feature if they want to
60 #include <libsigrok/libsigrok.h>
61 #include "libsigrok-internal.h"
63 /* TODO: Move these helpers to some library API routine group. */
64 struct sr_channel_group *sr_channel_group_new(const char *name, void *priv);
65 void sr_channel_group_free(struct sr_channel_group *cg);
67 #define LOG_PREFIX "input/logicport"
69 #define MAX_CHANNELS 34
70 #define CHUNK_SIZE (4 * 1024 * 1024)
73 #define DC1_CHR '\x11'
74 #define DC1_STR "\x11"
75 #define CONT_OPEN "/" DC1_STR
76 #define CONT_CLOSE DC1_STR "/"
79 * This is some heuristics (read: a HACK). The current implementation
80 * neither processes nor displays the user's notes, but takes their
81 * presence as a hint that all relevant input was seen, and sample data
82 * can get forwarded to the session bus.
84 #define LAST_KEYWORD "NotesString"
87 * The vendor software supports signal groups, and a single signal can
88 * be a member in multiple groups at the same time. The sigrok project
89 * does not support that configuration. Let's ignore the "All Signals"
90 * group by default, thus reducing the probability of a conflict.
92 #define SKIP_SIGNAL_GROUP "All Signals"
94 struct signal_group_desc {
101 gboolean ch_feed_prep;
102 gboolean header_sent;
107 size_t channel_count;
108 size_t sample_lines_total;
109 size_t sample_lines_read;
110 size_t sample_lines_fed;
111 uint64_t samples_got_uncomp;
114 SAMPLEDATA_OPEN_BRACE,
115 SAMPLEDATA_WIRES_COUNT,
116 SAMPLEDATA_DATA_LINES,
117 SAMPLEDATA_CLOSE_BRACE,
119 struct sample_data_entry {
122 } *sample_data_queue;
123 uint64_t sample_rate;
124 uint64_t wires_all_mask;
125 uint64_t wires_enabled;
126 uint64_t wires_inverted;
127 uint64_t wires_undefined;
128 char *wire_names[MAX_CHANNELS];
129 char *signal_names[MAX_CHANNELS];
130 uint64_t wires_grouped;
131 GSList *signal_groups;
134 size_t samples_per_chunk;
135 size_t samples_in_buffer;
136 uint8_t *feed_buffer;
139 static struct signal_group_desc *alloc_signal_group(const char *name)
141 struct signal_group_desc *desc;
143 desc = g_malloc0(sizeof(*desc));
145 desc->name = g_strdup(name);
150 static void free_signal_group(struct signal_group_desc *desc)
158 struct sr_channel_group *sr_channel_group_new(const char *name, void *priv)
160 struct sr_channel_group *cg;
162 cg = g_malloc0(sizeof(*cg));
164 cg->name = g_strdup(name);
170 void sr_channel_group_free(struct sr_channel_group *cg)
175 g_slist_free(cg->channels);
178 /* Wrapper for GDestroyNotify compatibility. */
179 static void sg_free(void *p)
181 return free_signal_group(p);
184 static int check_vers_line(char *line, int need_key,
185 gchar **version, gchar **build)
187 static const char *keyword = "Version";
188 static const char *caution = " CAUTION: Do not change the contents of this file.";
190 const char *prev_ptr;
198 /* Expect the 'Version' literal, followed by a DC1 separator. */
200 if (strncmp(read_ptr, keyword, strlen(keyword)) != 0)
202 read_ptr += strlen(keyword);
203 if (*read_ptr != DC1_CHR)
208 /* Expect some "\d+\.\d+" style version string and DC1. */
214 read_ptr += strspn(read_ptr, "0123456789.");
215 if (read_ptr == prev_ptr)
217 if (*read_ptr != DC1_CHR)
221 /* Expect some "\d+" style build number and DC1. */
227 read_ptr += strspn(read_ptr, "0123456789");
228 if (read_ptr == prev_ptr)
230 if (*read_ptr != DC1_CHR)
234 /* Expect the 'CAUTION...' text (weak test, only part of the text). */
235 if (strncmp(read_ptr, caution, strlen(caution)) != 0)
237 read_ptr += strlen(caution);
239 /* No check for CRLF, due to the weak CAUTION test. */
243 static int process_wire_names(struct context *inc, char **names)
248 * The 'names' array contains the *wire* names, plus a 'Count'
249 * label for the last column.
251 count = g_strv_length(names);
252 if (count != inc->channel_count + 1)
254 if (strcmp(names[inc->channel_count], "Count") != 0)
257 for (idx = 0; idx < inc->channel_count; idx++)
258 inc->wire_names[idx] = g_strdup(names[idx]);
263 static int process_signal_names(struct context *inc, char **names)
268 * The 'names' array contains the *signal* names (and no other
269 * entries, unlike the *wire* names).
271 count = g_strv_length(names);
272 if (count != inc->channel_count)
275 for (idx = 0; idx < inc->channel_count; idx++)
276 inc->signal_names[idx] = g_strdup(names[idx]);
281 static int process_signal_group(struct context *inc, char **args)
284 struct signal_group_desc *desc;
285 uint64_t bit_tmpl, bit_mask;
290 * List of arguments that we receive:
292 * - [1] - [5] uncertain meaning, four integers and one boolean
293 * - [6] comma separated list of wire indices (zero based)
294 * - [7] - [9] uncertain meaning, a boolean, two integers
295 * - [10] - [35] uncertain meaning, 26 empty columns
298 /* Check for the minimum amount of input data. */
301 if (g_strv_length(args) < 7)
306 /* Accept empty names and empty signal lists. Silently ignore. */
309 if (!wires || !*wires)
312 * TODO: Introduce a user configurable "ignore" option? Skip the
313 * "All Signals" group by default, and in addition whatever
314 * the user specified?
316 if (strcmp(name, SKIP_SIGNAL_GROUP) == 0) {
317 sr_info("Skipping signal group '%s'", name);
322 * Create the descriptor here to store the member list to. We
323 * cannot access signal names and sigrok channels yet, they
324 * only become avilable at a later point in time.
326 desc = alloc_signal_group(name);
328 return SR_ERR_MALLOC;
329 inc->signal_groups = g_slist_append(inc->signal_groups, desc);
332 * Determine the bit mask of the group's signals' indices.
334 * Implementation note: Use a "template" for a single bit, to
335 * avoid portability issues with upper bits. Without this 64bit
336 * intermediate variable, I would not know how to phrase e.g.
337 * (1ULL << 33) in portable, robust, and easy to maintain ways
338 * on all platforms that are supported by sigrok.
345 idx = strtoul(p, &endp, 0);
346 if (!endp || endp == p)
348 if (*endp && *endp != ',')
353 if (idx >= MAX_CHANNELS)
355 bit_mask = bit_tmpl << idx;
356 if (inc->wires_grouped & bit_mask) {
357 sr_warn("Not adding signal at index %zu to group %s (multiple assignments)",
360 desc->mask |= bit_mask;
361 inc->wires_grouped |= bit_mask;
364 sr_dbg("'Group' done, name '%s', mask 0x%" PRIx64 ".",
365 desc->name, desc->mask);
370 static int process_ungrouped_signals(struct context *inc)
373 struct signal_group_desc *desc;
376 * Only create the "ungrouped" channel group if there are any
377 * groups of other signals already.
379 if (!inc->signal_groups)
383 * Determine the bit mask of signals that are part of the
384 * acquisition and are not a member of any other group.
386 bit_mask = inc->wires_all_mask;
387 bit_mask &= inc->wires_enabled;
388 bit_mask &= ~inc->wires_grouped;
389 sr_dbg("'ungrouped' check: all 0x%" PRIx64 ", en 0x%" PRIx64 ", grp 0x%" PRIx64 " -> un 0x%" PRIx64 ".",
390 inc->wires_all_mask, inc->wires_enabled,
391 inc->wires_grouped, bit_mask);
395 /* Create a sigrok channel group without a name. */
396 desc = alloc_signal_group(NULL);
398 return SR_ERR_MALLOC;
399 inc->signal_groups = g_slist_append(inc->signal_groups, desc);
400 desc->mask = bit_mask;
405 static int process_enabled_channels(struct context *inc, char **flags)
411 * The 'flags' array contains (the textual representation of)
412 * the "enabled" state of the acquisition device's channels.
414 count = g_strv_length(flags);
415 if (count != inc->channel_count)
419 for (idx = 0; idx < inc->channel_count; idx++, mask <<= 1) {
420 if (strcmp(flags[idx], "True") == 0)
423 inc->wires_enabled = bits;
428 static int process_inverted_channels(struct context *inc, char **flags)
434 * The 'flags' array contains (the textual representation of)
435 * the "inverted" state of the acquisition device's channels.
437 count = g_strv_length(flags);
438 if (count != inc->channel_count)
442 for (idx = 0; idx < inc->channel_count; idx++, mask <<= 1) {
443 if (strcmp(flags[idx], "True") == 0)
446 inc->wires_inverted = bits;
451 static int process_sample_line(struct context *inc, char **values)
454 struct sample_data_entry *entry;
460 * The 'values' array contains '0'/'1' text representation of
461 * wire's values, as well as a (a textual representation of a)
462 * repeat counter for that set of samples.
464 count = g_strv_length(values);
465 if (count != inc->channel_count + 1)
467 entry = &inc->sample_data_queue[inc->sample_lines_read];
470 for (idx = 0; idx < inc->channel_count; idx++, mask <<= 1) {
471 if (strcmp(values[idx], "1") == 0)
473 if (strcmp(values[idx], "U") == 0)
474 inc->wires_undefined |= mask;
476 rc = sr_atol(values[inc->channel_count], &conv_ret);
479 entry->repeat = conv_ret;
480 inc->samples_got_uncomp += entry->repeat;
485 static int process_keyvalue_line(struct context *inc, char *line)
487 char *sep, *key, *arg;
490 char *version, *build;
494 double period, dbl_rate;
498 * Process lines of the 'SampleData' block. Inspection of the
499 * block got started below in the "regular keyword line" section.
500 * The code here handles the remaining number of lines: Opening
501 * and closing braces, wire names, and sample data sets. Note
502 * that the wire names and sample values are separated by comma,
503 * not by DC1 like other key/value pairs and argument lists.
505 switch (inc->in_sample_data) {
506 case SAMPLEDATA_OPEN_BRACE:
507 if (strcmp(line, "{") != 0)
509 inc->in_sample_data++;
511 case SAMPLEDATA_WIRES_COUNT:
512 while (isspace(*line))
514 args = g_strsplit(line, ",", 0);
515 rc = process_wire_names(inc, args);
519 inc->in_sample_data++;
520 inc->sample_lines_read = 0;
522 case SAMPLEDATA_DATA_LINES:
523 while (isspace(*line))
525 args = g_strsplit(line, ",", 0);
526 rc = process_sample_line(inc, args);
530 inc->sample_lines_read++;
531 if (inc->sample_lines_read == inc->sample_lines_total)
532 inc->in_sample_data++;
534 case SAMPLEDATA_CLOSE_BRACE:
535 if (strcmp(line, "}") != 0)
537 sr_dbg("'SampleData' done: samples count %" PRIu64 ".",
538 inc->samples_got_uncomp);
539 inc->sample_lines_fed = 0;
540 inc->in_sample_data = SAMPLEDATA_NONE;
542 case SAMPLEDATA_NONE:
543 /* EMPTY */ /* Fall through to regular keyword-line logic. */
547 /* Process regular key/value lines separated by DC1. */
549 sep = strchr(line, DC1_CHR);
554 if (strcmp(key, "Version") == 0) {
555 rc = check_vers_line(arg, 0, &version, &build);
557 inc->sw_version = g_strdup(version ? version : "?");
558 rc = sr_atol(build, &build_num);
559 inc->sw_build = build_num;
561 sr_dbg("'Version' line: version %s, build %zu.",
562 inc->sw_version, inc->sw_build);
565 if (strcmp(key, "AcquiredSamplePeriod") == 0) {
566 rc = sr_atod(arg, &period);
570 * Implementation detail: The vendor's software provides
571 * 1/2/5 choices in the 1kHz - 500MHz range. Unfortunately
572 * the choice of saving the sample _period_ as a floating
573 * point number in the text file yields inaccurate results
574 * for naive implementations of the conversion (0.1 is an
575 * "odd number" in the computer's internal representation).
576 * The below logic of rounding to integer and then rounding
577 * to full kHz works for the samplerate value's range.
578 * "Simplifying" the implementation will introduce errors.
580 dbl_rate = 1.0 / period;
581 int_rate = (uint64_t)(dbl_rate + 0.5);
585 inc->sample_rate = int_rate;
586 if (!inc->sample_rate)
588 sr_dbg("Sample rate: %" PRIu64 ".", inc->sample_rate);
591 if (strcmp(key, "AcquiredChannelList") == 0) {
592 args = g_strsplit(arg, DC1_STR, 0);
593 rc = process_enabled_channels(inc, args);
597 sr_dbg("Enabled channels: 0x%" PRIx64 ".",
601 if (strcmp(key, "InvertedChannelList") == 0) {
602 args = g_strsplit(arg, DC1_STR, 0);
603 rc = process_inverted_channels(inc, args);
605 sr_dbg("Inverted channels: 0x%" PRIx64 ".",
606 inc->wires_inverted);
609 if (strcmp(key, "Signals") == 0) {
610 args = g_strsplit(arg, DC1_STR, 0);
611 rc = process_signal_names(inc, args);
615 sr_dbg("Got signal names.");
618 if (strcmp(key, "SampleData") == 0) {
619 args = g_strsplit(arg, DC1_STR, 3);
620 if (!args || !args[0] || !args[1]) {
624 rc = sr_atoi(args[0], &wires);
629 rc = sr_atoi(args[1], &samples);
635 if (!wires || !samples)
637 inc->channel_count = wires;
638 inc->sample_lines_total = samples;
639 sr_dbg("'SampleData' start: wires %zu, sample lines %zu.",
640 inc->channel_count, inc->sample_lines_total);
641 if (inc->channel_count > MAX_CHANNELS)
643 inc->in_sample_data = SAMPLEDATA_OPEN_BRACE;
644 alloc_size = sizeof(inc->sample_data_queue[0]);
645 alloc_size *= inc->sample_lines_total;
646 inc->sample_data_queue = g_malloc0(alloc_size);
647 if (!inc->sample_data_queue)
649 inc->sample_lines_fed = 0;
652 if (strcmp(key, "Group") == 0) {
653 args = g_strsplit(arg, DC1_STR, 0);
654 rc = process_signal_group(inc, args);
660 if (strcmp(key, LAST_KEYWORD) == 0) {
661 sr_dbg("'" LAST_KEYWORD "' seen, assuming \"header done\".");
662 inc->got_header = TRUE;
666 /* Unsupported keyword, silently ignore the line. */
670 /* Check for, and isolate another line of text input. */
671 static int have_text_line(struct sr_input *in, char **line, char **next)
673 char *sol_ptr, *eol_ptr;
675 if (!in || !in->buf || !in->buf->str)
677 sol_ptr = in->buf->str;
678 eol_ptr = strstr(sol_ptr, CRLF);
684 eol_ptr += strlen(CRLF);
691 /* Handle line continuation. Have logical lines processed. */
692 static int process_text_line(struct context *inc, char *line)
699 * Handle line continuation in the input stream. Notice that
700 * continued lines can start and end on the same input line.
701 * The text between the markers can be empty, too.
703 * Make the result look like a regular line. Put a DC1 delimiter
704 * between the keyword and the right hand side. Strip the /<DC1>
705 * and <DC1>/ "braces". Put CRLF between all continued parts,
706 * this makes the data appear "most intuitive and natural"
707 * should we e.g. pass on user's notes in a future version.
710 if (!inc->cont_buff) {
711 p = strstr(line, CONT_OPEN);
713 /* Start of continuation. */
714 inc->cont_buff = g_string_new_len(line, p - line + 1);
715 inc->cont_buff->str[inc->cont_buff->len - 1] = DC1_CHR;
716 line = p + strlen(CONT_OPEN);
718 /* Regular line, fall through to below regular logic. */
720 if (inc->cont_buff) {
721 p = strstr(line, CONT_CLOSE);
722 is_cont_end = p != NULL;
725 g_string_append_len(inc->cont_buff, line, strlen(line));
727 /* Keep accumulating. */
728 g_string_append_len(inc->cont_buff, CRLF, strlen(CRLF));
731 /* End of continuation. */
732 line = inc->cont_buff->str;
736 * Process a logical line of input. It either was received from
737 * the caller, or is the result of accumulating continued lines.
739 rc = process_keyvalue_line(inc, line);
741 /* Release the accumulation buffer when a continuation ended. */
743 g_string_free(inc->cont_buff, TRUE);
744 inc->cont_buff = NULL;
750 /* Tell whether received data is sufficient for session feed preparation. */
751 static int have_header(GString *buf)
753 const char *assumed_last_key = CRLF LAST_KEYWORD CONT_OPEN;
755 if (strstr(buf->str, assumed_last_key))
761 /* Process/inspect previously received input data. Get header parameters. */
762 static int parse_header(struct sr_input *in)
769 while (have_text_line(in, &line, &next)) {
770 rc = process_text_line(inc, line);
771 g_string_erase(in->buf, 0, next - line);
779 /* Create sigrok channels and groups. Allocate the session feed buffer. */
780 static int create_channels_groups_buffer(struct sr_input *in)
787 struct sr_channel *ch;
788 struct sr_dev_inst *sdi;
790 struct signal_group_desc *desc;
791 struct sr_channel_group *cg;
796 for (idx = 0; idx < inc->channel_count; idx++, mask <<= 1) {
797 name = inc->signal_names[idx];
799 name = inc->wire_names[idx];
800 enabled = (inc->wires_enabled & mask) ? TRUE : FALSE;
801 ch = sr_channel_new(in->sdi, idx,
802 SR_CHANNEL_LOGIC, enabled, name);
804 return SR_ERR_MALLOC;
805 inc->channels = g_slist_append(inc->channels, ch);
809 for (l = inc->signal_groups; l; l = l->next) {
811 cg = sr_channel_group_new(desc->name, NULL);
813 return SR_ERR_MALLOC;
814 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
816 for (idx = 0; idx < inc->channel_count; idx++, mask <<= 1) {
817 if (!(desc->mask & mask))
819 ch = g_slist_nth_data(inc->channels, idx);
822 cg->channels = g_slist_append(cg->channels, ch);
826 inc->unitsize = (inc->channel_count + 7) / 8;
827 inc->samples_per_chunk = CHUNK_SIZE / inc->unitsize;
828 inc->samples_in_buffer = 0;
829 inc->feed_buffer = g_malloc0(inc->samples_per_chunk * inc->unitsize);
830 if (!inc->feed_buffer)
831 return SR_ERR_MALLOC;
836 /* Send all accumulated sample data values to the session. */
837 static int send_buffer(struct sr_input *in)
840 struct sr_datafeed_packet packet;
841 struct sr_datafeed_meta meta;
842 struct sr_config *src;
843 struct sr_datafeed_logic logic;
847 if (!inc->samples_in_buffer)
850 if (!inc->header_sent) {
851 rc = std_session_send_df_header(in->sdi);
854 inc->header_sent = TRUE;
857 if (inc->sample_rate && !inc->rate_sent) {
858 packet.type = SR_DF_META;
859 packet.payload = &meta;
860 src = sr_config_new(SR_CONF_SAMPLERATE,
861 g_variant_new_uint64(inc->sample_rate));
862 meta.config = g_slist_append(NULL, src);
863 rc = sr_session_send(in->sdi, &packet);
864 g_slist_free(meta.config);
868 inc->rate_sent = TRUE;
871 packet.type = SR_DF_LOGIC;
872 packet.payload = &logic;
873 logic.unitsize = inc->unitsize;
874 logic.data = inc->feed_buffer;
875 logic.length = inc->unitsize * inc->samples_in_buffer;
876 rc = sr_session_send(in->sdi, &packet);
878 inc->samples_in_buffer = 0;
887 * Add N copies of the current sample to the buffer. Send the buffer to
888 * the session feed when a maximum amount of data was collected.
890 static int add_samples(struct sr_input *in, uint64_t samples, size_t count)
893 uint8_t sample_buffer[sizeof(uint64_t)];
900 for (idx = 0; idx < inc->unitsize; idx++) {
901 sample_buffer[idx] = samples & 0xff;
905 copy_count = inc->samples_per_chunk - inc->samples_in_buffer;
906 if (copy_count > count)
910 p = inc->feed_buffer + inc->samples_in_buffer * inc->unitsize;
911 while (copy_count-- > 0) {
912 memcpy(p, sample_buffer, inc->unitsize);
914 inc->samples_in_buffer++;
917 if (inc->samples_in_buffer == inc->samples_per_chunk) {
918 rc = send_buffer(in);
927 /* Pass on previously received samples to the session. */
928 static int process_queued_samples(struct sr_input *in)
931 struct sample_data_entry *entry;
932 uint64_t sample_bits;
936 while (inc->sample_lines_fed < inc->sample_lines_total) {
937 entry = &inc->sample_data_queue[inc->sample_lines_fed++];
938 sample_bits = entry->bits;
939 sample_bits ^= inc->wires_inverted;
940 sample_bits &= inc->wires_enabled;
941 rc = add_samples(in, sample_bits, entry->repeat);
950 * Create required resources between having read the input file and
951 * sending sample data to the session. Send initial packets before
952 * sample data follows.
954 static int prepare_session_feed(struct sr_input *in)
960 if (inc->ch_feed_prep)
963 /* Got channel names? At least fallbacks? */
964 if (!inc->wire_names[0] || !inc->wire_names[0][0])
966 /* Samples seen? Seen them all? */
967 if (!inc->channel_count)
969 if (!inc->sample_lines_total)
971 if (inc->in_sample_data)
973 if (!inc->sample_data_queue)
975 inc->sample_lines_fed = 0;
978 * Normalize some variants of input data.
979 * - Let's create a mask for the maximum possible
980 * bit positions, it will be useful to avoid garbage
981 * in other code paths, too.
982 * - Input files _might_ specify which channels were
983 * enabled during acquisition. _Or_ not specify the
984 * enabled channels, but provide 'U' values in some
985 * columns. When neither was seen, assume that all
986 * channels are enabled.
987 * - If there are any signal groups, put all signals into
988 * an anonymous group that are not part of another group.
990 inc->wires_all_mask = 1UL << 0;
991 inc->wires_all_mask <<= inc->channel_count;
992 inc->wires_all_mask--;
993 sr_dbg("all wires mask: 0x%" PRIx64 ".", inc->wires_all_mask);
994 if (!inc->wires_enabled) {
995 inc->wires_enabled = ~inc->wires_undefined;
996 inc->wires_enabled &= ~inc->wires_all_mask;
997 sr_dbg("enabled from undefined: 0x%" PRIx64 ".",
1000 if (!inc->wires_enabled) {
1001 inc->wires_enabled = inc->wires_all_mask;
1002 sr_dbg("enabled from total mask: 0x%" PRIx64 ".",
1003 inc->wires_enabled);
1005 sr_dbg("enabled mask: 0x%" PRIx64 ".",
1006 inc->wires_enabled);
1007 rc = process_ungrouped_signals(inc);
1012 * "Start" the session: Create channels, send the DF
1013 * header to the session. Optionally send the sample
1014 * rate before sample data will be sent.
1016 rc = create_channels_groups_buffer(in);
1020 inc->ch_feed_prep = TRUE;
1025 static int format_match(GHashTable *metadata)
1027 GString *buf, *tmpbuf;
1029 gchar *version, *build;
1031 /* Get a copy of the start of the file's content. */
1032 buf = g_hash_table_lookup(metadata, GINT_TO_POINTER(SR_INPUT_META_HEADER));
1033 if (!buf || !buf->str)
1035 tmpbuf = g_string_new_len(buf->str, buf->len);
1036 if (!tmpbuf || !tmpbuf->str)
1037 return SR_ERR_MALLOC;
1039 /* See if we can spot a typical first LPF line. */
1040 rc = check_vers_line(tmpbuf->str, 1, &version, &build);
1041 if (rc == SR_OK && version && build) {
1042 sr_dbg("Looks like a LogicProbe project, version %s, build %s.",
1045 g_string_free(tmpbuf, TRUE);
1050 static int init(struct sr_input *in, GHashTable *options)
1052 struct context *inc;
1056 in->sdi = g_malloc0(sizeof(*in->sdi));
1057 inc = g_malloc0(sizeof(*inc));
1063 static int receive(struct sr_input *in, GString *buf)
1065 struct context *inc;
1068 /* Accumulate another chunk of input data. */
1069 g_string_append_len(in->buf, buf->str, buf->len);
1072 * Wait for the full header's availability, then process it in a
1073 * single call, and set the "ready" flag. Make sure sample data
1074 * and the header get processed in disjoint calls to receive(),
1075 * the backend requires those separate phases.
1078 if (!inc->got_header) {
1079 if (!have_header(in->buf))
1081 rc = parse_header(in);
1084 rc = prepare_session_feed(in);
1087 in->sdi_ready = TRUE;
1091 /* Process sample data, after the header got processed. */
1092 rc = process_queued_samples(in);
1097 static int end(struct sr_input *in)
1099 struct context *inc;
1102 /* Nothing to do here if we never started feeding the session. */
1107 * Process sample data that may not have been forwarded before.
1108 * Flush any potentially queued samples.
1110 rc = process_queued_samples(in);
1113 rc = send_buffer(in);
1117 /* End the session feed if one was started. */
1119 if (inc->header_sent) {
1120 rc = std_session_send_df_end(in->sdi);
1121 inc->header_sent = FALSE;
1127 static void cleanup(struct sr_input *in)
1129 struct context *inc;
1140 * Release potentially allocated resources. Void all references
1141 * and scalars, so that re-runs start out fresh again.
1143 g_free(inc->sw_version);
1144 g_string_free(inc->cont_buff, TRUE);
1145 g_free(inc->sample_data_queue);
1146 for (idx = 0; idx < inc->channel_count; idx++)
1147 g_free(inc->wire_names[idx]);
1148 for (idx = 0; idx < inc->channel_count; idx++)
1149 g_free(inc->signal_names[idx]);
1150 g_slist_free_full(inc->signal_groups, sg_free);
1151 g_slist_free_full(inc->channels, g_free);
1152 g_free(inc->feed_buffer);
1153 memset(inc, 0, sizeof(*inc));
1156 static int reset(struct sr_input *in)
1158 struct context *inc;
1162 inc->ch_feed_prep = FALSE;
1163 inc->header_sent = FALSE;
1164 inc->rate_sent = FALSE;
1165 g_string_truncate(in->buf, 0);
1170 static struct sr_option options[] = {
1174 static const struct sr_option *get_options(void)
1179 SR_PRIV struct sr_input_module input_logicport = {
1181 .name = "LogicPort File",
1182 .desc = "Intronix LA1034 LogicPort project",
1183 .exts = (const char *[]){ "lpf", NULL },
1184 .metadata = { SR_INPUT_META_HEADER | SR_INPUT_META_REQUIRED },
1185 .options = get_options,
1186 .format_match = format_match,