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 #define LOG_PREFIX "input/logicport"
65 #define MAX_CHANNELS 34
66 #define CHUNK_SIZE (4 * 1024 * 1024)
69 #define DC1_CHR '\x11'
70 #define DC1_STR "\x11"
71 #define CONT_OPEN "/" DC1_STR
72 #define CONT_CLOSE DC1_STR "/"
75 * This is some heuristics (read: a HACK). The current implementation
76 * neither processes nor displays the user's notes, but takes their
77 * presence as a hint that all relevant input was seen, and sample data
78 * can get forwarded to the session bus.
80 #define LAST_KEYWORD "NotesString"
83 * The vendor software supports signal groups, and a single signal can
84 * be a member in multiple groups at the same time. The sigrok project
85 * does not support that configuration. Let's ignore the "All Signals"
86 * group by default, thus reducing the probability of a conflict.
88 #define SKIP_SIGNAL_GROUP "All Signals"
90 struct signal_group_desc {
97 gboolean ch_feed_prep;
103 size_t channel_count;
104 size_t sample_lines_total;
105 size_t sample_lines_read;
106 size_t sample_lines_fed;
107 uint64_t samples_got_uncomp;
110 SAMPLEDATA_OPEN_BRACE,
111 SAMPLEDATA_WIRES_COUNT,
112 SAMPLEDATA_DATA_LINES,
113 SAMPLEDATA_CLOSE_BRACE,
115 struct sample_data_entry {
118 } *sample_data_queue;
119 uint64_t sample_rate;
120 uint64_t wires_all_mask;
121 uint64_t wires_enabled;
122 uint64_t wires_inverted;
123 uint64_t wires_undefined;
124 char *wire_names[MAX_CHANNELS];
125 char *signal_names[MAX_CHANNELS];
126 uint64_t wires_grouped;
127 GSList *signal_groups;
130 size_t samples_per_chunk;
131 size_t samples_in_buffer;
132 uint8_t *feed_buffer;
135 static struct signal_group_desc *alloc_signal_group(const char *name)
137 struct signal_group_desc *desc;
139 desc = g_malloc0(sizeof(*desc));
141 desc->name = g_strdup(name);
146 static void free_signal_group(struct signal_group_desc *desc)
154 /* Wrapper for GDestroyNotify compatibility. */
155 static void sg_free(void *p)
157 return free_signal_group(p);
160 static int check_vers_line(char *line, int need_key,
161 gchar **version, gchar **build)
163 static const char *keyword = "Version";
164 static const char *caution = " CAUTION: Do not change the contents of this file.";
166 const char *prev_ptr;
174 /* Expect the 'Version' literal, followed by a DC1 separator. */
176 if (strncmp(read_ptr, keyword, strlen(keyword)) != 0)
178 read_ptr += strlen(keyword);
179 if (*read_ptr != DC1_CHR)
184 /* Expect some "\d+\.\d+" style version string and DC1. */
190 read_ptr += strspn(read_ptr, "0123456789.");
191 if (read_ptr == prev_ptr)
193 if (*read_ptr != DC1_CHR)
197 /* Expect some "\d+" style build number and DC1. */
203 read_ptr += strspn(read_ptr, "0123456789");
204 if (read_ptr == prev_ptr)
206 if (*read_ptr != DC1_CHR)
210 /* Expect the 'CAUTION...' text (weak test, only part of the text). */
211 if (strncmp(read_ptr, caution, strlen(caution)) != 0)
213 read_ptr += strlen(caution);
215 /* No check for CRLF, due to the weak CAUTION test. */
219 static int process_wire_names(struct context *inc, char **names)
224 * The 'names' array contains the *wire* names, plus a 'Count'
225 * label for the last column.
227 count = g_strv_length(names);
228 if (count != inc->channel_count + 1)
230 if (strcmp(names[inc->channel_count], "Count") != 0)
233 for (idx = 0; idx < inc->channel_count; idx++)
234 inc->wire_names[idx] = g_strdup(names[idx]);
239 static int process_signal_names(struct context *inc, char **names)
244 * The 'names' array contains the *signal* names (and no other
245 * entries, unlike the *wire* names).
247 count = g_strv_length(names);
248 if (count != inc->channel_count)
251 for (idx = 0; idx < inc->channel_count; idx++)
252 inc->signal_names[idx] = g_strdup(names[idx]);
257 static int process_signal_group(struct context *inc, char **args)
260 struct signal_group_desc *desc;
266 * List of arguments that we receive:
268 * - [1] - [5] uncertain meaning, four integers and one boolean
269 * - [6] comma separated list of wire indices (zero based)
270 * - [7] - [9] uncertain meaning, a boolean, two integers
271 * - [10] - [35] uncertain meaning, 26 empty columns
274 /* Check for the minimum amount of input data. */
277 if (g_strv_length(args) < 7)
282 /* Accept empty names and empty signal lists. Silently ignore. */
285 if (!wires || !*wires)
288 * TODO: Introduce a user configurable "ignore" option? Skip the
289 * "All Signals" group by default, and in addition whatever
290 * the user specified?
292 if (strcmp(name, SKIP_SIGNAL_GROUP) == 0) {
293 sr_info("Skipping signal group '%s'", name);
298 * Create the descriptor here to store the member list to. We
299 * cannot access signal names and sigrok channels yet, they
300 * only become avilable at a later point in time.
302 desc = alloc_signal_group(name);
304 return SR_ERR_MALLOC;
305 inc->signal_groups = g_slist_append(inc->signal_groups, desc);
307 /* Determine the bit mask of the group's signals' indices. */
312 idx = strtoul(p, &endp, 0);
313 if (!endp || endp == p)
315 if (*endp && *endp != ',')
320 if (idx >= MAX_CHANNELS)
322 bit_mask = UINT64_C(1) << idx;
323 if (inc->wires_grouped & bit_mask) {
324 sr_warn("Not adding signal at index %zu to group %s (multiple assignments)",
327 desc->mask |= bit_mask;
328 inc->wires_grouped |= bit_mask;
331 sr_dbg("'Group' done, name '%s', mask 0x%" PRIx64 ".",
332 desc->name, desc->mask);
337 static int process_ungrouped_signals(struct context *inc)
340 struct signal_group_desc *desc;
343 * Only create the "ungrouped" channel group if there are any
344 * groups of other signals already.
346 if (!inc->signal_groups)
350 * Determine the bit mask of signals that are part of the
351 * acquisition and are not a member of any other group.
353 bit_mask = inc->wires_all_mask;
354 bit_mask &= inc->wires_enabled;
355 bit_mask &= ~inc->wires_grouped;
356 sr_dbg("'ungrouped' check: all 0x%" PRIx64 ", en 0x%" PRIx64 ", grp 0x%" PRIx64 " -> un 0x%" PRIx64 ".",
357 inc->wires_all_mask, inc->wires_enabled,
358 inc->wires_grouped, bit_mask);
362 /* Create a sigrok channel group without a name. */
363 desc = alloc_signal_group(NULL);
365 return SR_ERR_MALLOC;
366 inc->signal_groups = g_slist_append(inc->signal_groups, desc);
367 desc->mask = bit_mask;
372 static int process_enabled_channels(struct context *inc, char **flags)
378 * The 'flags' array contains (the textual representation of)
379 * the "enabled" state of the acquisition device's channels.
381 count = g_strv_length(flags);
382 if (count != inc->channel_count)
386 for (idx = 0; idx < inc->channel_count; idx++, mask <<= 1) {
387 if (strcmp(flags[idx], "True") == 0)
390 inc->wires_enabled = bits;
395 static int process_inverted_channels(struct context *inc, char **flags)
401 * The 'flags' array contains (the textual representation of)
402 * the "inverted" state of the acquisition device's channels.
404 count = g_strv_length(flags);
405 if (count != inc->channel_count)
409 for (idx = 0; idx < inc->channel_count; idx++, mask <<= 1) {
410 if (strcmp(flags[idx], "True") == 0)
413 inc->wires_inverted = bits;
418 static int process_sample_line(struct context *inc, char **values)
421 struct sample_data_entry *entry;
427 * The 'values' array contains '0'/'1' text representation of
428 * wire's values, as well as a (a textual representation of a)
429 * repeat counter for that set of samples.
431 count = g_strv_length(values);
432 if (count != inc->channel_count + 1)
434 entry = &inc->sample_data_queue[inc->sample_lines_read];
437 for (idx = 0; idx < inc->channel_count; idx++, mask <<= 1) {
438 if (strcmp(values[idx], "1") == 0)
440 if (strcmp(values[idx], "U") == 0)
441 inc->wires_undefined |= mask;
443 rc = sr_atol(values[inc->channel_count], &conv_ret);
446 entry->repeat = conv_ret;
447 inc->samples_got_uncomp += entry->repeat;
452 static int process_keyvalue_line(struct context *inc, char *line)
454 char *sep, *key, *arg;
457 char *version, *build;
461 double period, dbl_rate;
465 * Process lines of the 'SampleData' block. Inspection of the
466 * block got started below in the "regular keyword line" section.
467 * The code here handles the remaining number of lines: Opening
468 * and closing braces, wire names, and sample data sets. Note
469 * that the wire names and sample values are separated by comma,
470 * not by DC1 like other key/value pairs and argument lists.
472 switch (inc->in_sample_data) {
473 case SAMPLEDATA_OPEN_BRACE:
474 if (strcmp(line, "{") != 0)
476 inc->in_sample_data++;
478 case SAMPLEDATA_WIRES_COUNT:
479 while (isspace(*line))
481 args = g_strsplit(line, ",", 0);
482 rc = process_wire_names(inc, args);
486 inc->in_sample_data++;
487 inc->sample_lines_read = 0;
489 case SAMPLEDATA_DATA_LINES:
490 while (isspace(*line))
492 args = g_strsplit(line, ",", 0);
493 rc = process_sample_line(inc, args);
497 inc->sample_lines_read++;
498 if (inc->sample_lines_read == inc->sample_lines_total)
499 inc->in_sample_data++;
501 case SAMPLEDATA_CLOSE_BRACE:
502 if (strcmp(line, "}") != 0)
504 sr_dbg("'SampleData' done: samples count %" PRIu64 ".",
505 inc->samples_got_uncomp);
506 inc->sample_lines_fed = 0;
507 inc->in_sample_data = SAMPLEDATA_NONE;
509 case SAMPLEDATA_NONE:
510 /* EMPTY */ /* Fall through to regular keyword-line logic. */
514 /* Process regular key/value lines separated by DC1. */
516 sep = strchr(line, DC1_CHR);
521 if (strcmp(key, "Version") == 0) {
522 rc = check_vers_line(arg, 0, &version, &build);
524 inc->sw_version = g_strdup(version ? version : "?");
525 rc = sr_atol(build, &build_num);
526 inc->sw_build = build_num;
528 sr_dbg("'Version' line: version %s, build %zu.",
529 inc->sw_version, inc->sw_build);
532 if (strcmp(key, "AcquiredSamplePeriod") == 0) {
533 rc = sr_atod(arg, &period);
537 * Implementation detail: The vendor's software provides
538 * 1/2/5 choices in the 1kHz - 500MHz range. Unfortunately
539 * the choice of saving the sample _period_ as a floating
540 * point number in the text file yields inaccurate results
541 * for naive implementations of the conversion (0.1 is an
542 * "odd number" in the computer's internal representation).
543 * The below logic of rounding to integer and then rounding
544 * to full kHz works for the samplerate value's range.
545 * "Simplifying" the implementation will introduce errors.
547 dbl_rate = 1.0 / period;
548 int_rate = (uint64_t)(dbl_rate + 0.5);
552 inc->sample_rate = int_rate;
553 if (!inc->sample_rate)
555 sr_dbg("Sample rate: %" PRIu64 ".", inc->sample_rate);
558 if (strcmp(key, "AcquiredChannelList") == 0) {
559 args = g_strsplit(arg, DC1_STR, 0);
560 rc = process_enabled_channels(inc, args);
564 sr_dbg("Enabled channels: 0x%" PRIx64 ".",
568 if (strcmp(key, "InvertedChannelList") == 0) {
569 args = g_strsplit(arg, DC1_STR, 0);
570 rc = process_inverted_channels(inc, args);
572 sr_dbg("Inverted channels: 0x%" PRIx64 ".",
573 inc->wires_inverted);
576 if (strcmp(key, "Signals") == 0) {
577 args = g_strsplit(arg, DC1_STR, 0);
578 rc = process_signal_names(inc, args);
582 sr_dbg("Got signal names.");
585 if (strcmp(key, "SampleData") == 0) {
586 args = g_strsplit(arg, DC1_STR, 3);
587 if (!args || !args[0] || !args[1]) {
591 rc = sr_atoi(args[0], &wires);
596 rc = sr_atoi(args[1], &samples);
602 if (!wires || !samples)
604 inc->channel_count = wires;
605 inc->sample_lines_total = samples;
606 sr_dbg("'SampleData' start: wires %zu, sample lines %zu.",
607 inc->channel_count, inc->sample_lines_total);
608 if (inc->channel_count > MAX_CHANNELS)
610 inc->in_sample_data = SAMPLEDATA_OPEN_BRACE;
611 alloc_size = sizeof(inc->sample_data_queue[0]);
612 alloc_size *= inc->sample_lines_total;
613 inc->sample_data_queue = g_malloc0(alloc_size);
614 if (!inc->sample_data_queue)
616 inc->sample_lines_fed = 0;
619 if (strcmp(key, "Group") == 0) {
620 args = g_strsplit(arg, DC1_STR, 0);
621 rc = process_signal_group(inc, args);
627 if (strcmp(key, LAST_KEYWORD) == 0) {
628 sr_dbg("'" LAST_KEYWORD "' seen, assuming \"header done\".");
629 inc->got_header = TRUE;
633 /* Unsupported keyword, silently ignore the line. */
637 /* Check for, and isolate another line of text input. */
638 static int have_text_line(struct sr_input *in, char **line, char **next)
640 char *sol_ptr, *eol_ptr;
642 if (!in || !in->buf || !in->buf->str)
644 sol_ptr = in->buf->str;
645 eol_ptr = strstr(sol_ptr, CRLF);
651 eol_ptr += strlen(CRLF);
658 /* Handle line continuation. Have logical lines processed. */
659 static int process_text_line(struct context *inc, char *line)
666 * Handle line continuation in the input stream. Notice that
667 * continued lines can start and end on the same input line.
668 * The text between the markers can be empty, too.
670 * Make the result look like a regular line. Put a DC1 delimiter
671 * between the keyword and the right hand side. Strip the /<DC1>
672 * and <DC1>/ "braces". Put CRLF between all continued parts,
673 * this makes the data appear "most intuitive and natural"
674 * should we e.g. pass on user's notes in a future version.
677 if (!inc->cont_buff) {
678 p = strstr(line, CONT_OPEN);
680 /* Start of continuation. */
681 inc->cont_buff = g_string_new_len(line, p - line + 1);
682 inc->cont_buff->str[inc->cont_buff->len - 1] = DC1_CHR;
683 line = p + strlen(CONT_OPEN);
685 /* Regular line, fall through to below regular logic. */
687 if (inc->cont_buff) {
688 p = strstr(line, CONT_CLOSE);
689 is_cont_end = p != NULL;
692 g_string_append_len(inc->cont_buff, line, strlen(line));
694 /* Keep accumulating. */
695 g_string_append_len(inc->cont_buff, CRLF, strlen(CRLF));
698 /* End of continuation. */
699 line = inc->cont_buff->str;
703 * Process a logical line of input. It either was received from
704 * the caller, or is the result of accumulating continued lines.
706 rc = process_keyvalue_line(inc, line);
708 /* Release the accumulation buffer when a continuation ended. */
710 g_string_free(inc->cont_buff, TRUE);
711 inc->cont_buff = NULL;
717 /* Tell whether received data is sufficient for session feed preparation. */
718 static int have_header(GString *buf)
720 const char *assumed_last_key = CRLF LAST_KEYWORD CONT_OPEN;
722 if (strstr(buf->str, assumed_last_key))
728 /* Process/inspect previously received input data. Get header parameters. */
729 static int parse_header(struct sr_input *in)
736 while (have_text_line(in, &line, &next)) {
737 rc = process_text_line(inc, line);
738 g_string_erase(in->buf, 0, next - line);
746 /* Create sigrok channels and groups. */
747 static int create_channels_groups(struct sr_input *in)
754 struct sr_channel *ch;
755 struct sr_dev_inst *sdi;
757 struct signal_group_desc *desc;
758 struct sr_channel_group *cg;
766 for (idx = 0; idx < inc->channel_count; idx++, mask <<= 1) {
767 name = inc->signal_names[idx];
769 name = inc->wire_names[idx];
770 enabled = (inc->wires_enabled & mask) ? TRUE : FALSE;
771 ch = sr_channel_new(in->sdi, idx,
772 SR_CHANNEL_LOGIC, enabled, name);
774 return SR_ERR_MALLOC;
775 inc->channels = g_slist_append(inc->channels, ch);
779 for (l = inc->signal_groups; l; l = l->next) {
781 cg = sr_channel_group_new(sdi, desc->name, NULL);
783 return SR_ERR_MALLOC;
785 for (idx = 0; idx < inc->channel_count; idx++, mask <<= 1) {
786 if (!(desc->mask & mask))
788 ch = g_slist_nth_data(inc->channels, idx);
791 cg->channels = g_slist_append(cg->channels, ch);
798 /* Allocate the session feed buffer. */
799 static int create_feed_buffer(struct sr_input *in)
805 inc->unitsize = (inc->channel_count + 7) / 8;
806 inc->samples_per_chunk = CHUNK_SIZE / inc->unitsize;
807 inc->samples_in_buffer = 0;
808 inc->feed_buffer = g_malloc0(inc->samples_per_chunk * inc->unitsize);
809 if (!inc->feed_buffer)
810 return SR_ERR_MALLOC;
815 /* Send all accumulated sample data values to the session. */
816 static int send_buffer(struct sr_input *in)
819 struct sr_datafeed_packet packet;
820 struct sr_datafeed_logic logic;
824 if (!inc->samples_in_buffer)
827 if (!inc->header_sent) {
828 rc = std_session_send_df_header(in->sdi);
831 inc->header_sent = TRUE;
834 if (inc->sample_rate && !inc->rate_sent) {
835 rc = sr_session_send_meta(in->sdi, SR_CONF_SAMPLERATE,
836 g_variant_new_uint64(inc->sample_rate));
839 inc->rate_sent = TRUE;
842 packet.type = SR_DF_LOGIC;
843 packet.payload = &logic;
844 logic.unitsize = inc->unitsize;
845 logic.data = inc->feed_buffer;
846 logic.length = inc->unitsize * inc->samples_in_buffer;
847 rc = sr_session_send(in->sdi, &packet);
849 inc->samples_in_buffer = 0;
858 * Add N copies of the current sample to the buffer. Send the buffer to
859 * the session feed when a maximum amount of data was collected.
861 static int add_samples(struct sr_input *in, uint64_t samples, size_t count)
864 uint8_t sample_buffer[sizeof(uint64_t)];
871 for (idx = 0; idx < inc->unitsize; idx++) {
872 sample_buffer[idx] = samples & 0xff;
876 copy_count = inc->samples_per_chunk - inc->samples_in_buffer;
877 if (copy_count > count)
881 p = inc->feed_buffer + inc->samples_in_buffer * inc->unitsize;
882 while (copy_count-- > 0) {
883 memcpy(p, sample_buffer, inc->unitsize);
885 inc->samples_in_buffer++;
888 if (inc->samples_in_buffer == inc->samples_per_chunk) {
889 rc = send_buffer(in);
898 /* Pass on previously received samples to the session. */
899 static int process_queued_samples(struct sr_input *in)
902 struct sample_data_entry *entry;
903 uint64_t sample_bits;
907 while (inc->sample_lines_fed < inc->sample_lines_total) {
908 entry = &inc->sample_data_queue[inc->sample_lines_fed++];
909 sample_bits = entry->bits;
910 sample_bits ^= inc->wires_inverted;
911 sample_bits &= inc->wires_enabled;
912 rc = add_samples(in, sample_bits, entry->repeat);
921 * Create required resources between having read the input file and
922 * sending sample data to the session. Send initial packets before
923 * sample data follows.
925 static int prepare_session_feed(struct sr_input *in)
931 if (inc->ch_feed_prep)
934 /* Got channel names? At least fallbacks? */
935 if (!inc->wire_names[0] || !inc->wire_names[0][0])
937 /* Samples seen? Seen them all? */
938 if (!inc->channel_count)
940 if (!inc->sample_lines_total)
942 if (inc->in_sample_data)
944 if (!inc->sample_data_queue)
946 inc->sample_lines_fed = 0;
949 * Normalize some variants of input data.
950 * - Let's create a mask for the maximum possible
951 * bit positions, it will be useful to avoid garbage
952 * in other code paths, too.
953 * - Input files _might_ specify which channels were
954 * enabled during acquisition. _Or_ not specify the
955 * enabled channels, but provide 'U' values in some
956 * columns. When neither was seen, assume that all
957 * channels are enabled.
958 * - If there are any signal groups, put all signals into
959 * an anonymous group that are not part of another group.
961 inc->wires_all_mask = UINT64_C(1);
962 inc->wires_all_mask <<= inc->channel_count;
963 inc->wires_all_mask--;
964 sr_dbg("all wires mask: 0x%" PRIx64 ".", inc->wires_all_mask);
965 if (!inc->wires_enabled) {
966 inc->wires_enabled = ~inc->wires_undefined;
967 inc->wires_enabled &= ~inc->wires_all_mask;
968 sr_dbg("enabled from undefined: 0x%" PRIx64 ".",
971 if (!inc->wires_enabled) {
972 inc->wires_enabled = inc->wires_all_mask;
973 sr_dbg("enabled from total mask: 0x%" PRIx64 ".",
976 sr_dbg("enabled mask: 0x%" PRIx64 ".",
978 rc = process_ungrouped_signals(inc);
983 * "Start" the session: Create channels, send the DF
984 * header to the session. Optionally send the sample
985 * rate before sample data will be sent.
987 rc = create_channels_groups(in);
990 rc = create_feed_buffer(in);
994 inc->ch_feed_prep = TRUE;
999 static int format_match(GHashTable *metadata, unsigned int *confidence)
1001 GString *buf, *tmpbuf;
1003 gchar *version, *build;
1005 /* Get a copy of the start of the file's content. */
1006 buf = g_hash_table_lookup(metadata, GINT_TO_POINTER(SR_INPUT_META_HEADER));
1007 if (!buf || !buf->str)
1009 tmpbuf = g_string_new_len(buf->str, buf->len);
1010 if (!tmpbuf || !tmpbuf->str)
1011 return SR_ERR_MALLOC;
1013 /* See if we can spot a typical first LPF line. */
1014 rc = check_vers_line(tmpbuf->str, 1, &version, &build);
1015 if (rc == SR_OK && version && build) {
1016 sr_dbg("Looks like a LogicProbe project, version %s, build %s.",
1020 g_string_free(tmpbuf, TRUE);
1025 static int init(struct sr_input *in, GHashTable *options)
1027 struct context *inc;
1031 in->sdi = g_malloc0(sizeof(*in->sdi));
1032 inc = g_malloc0(sizeof(*inc));
1038 static int receive(struct sr_input *in, GString *buf)
1040 struct context *inc;
1043 /* Accumulate another chunk of input data. */
1044 g_string_append_len(in->buf, buf->str, buf->len);
1047 * Wait for the full header's availability, then process it in a
1048 * single call, and set the "ready" flag. Make sure sample data
1049 * and the header get processed in disjoint calls to receive(),
1050 * the backend requires those separate phases.
1053 if (!inc->got_header) {
1054 if (!have_header(in->buf))
1056 rc = parse_header(in);
1059 rc = prepare_session_feed(in);
1062 in->sdi_ready = TRUE;
1066 /* Process sample data, after the header got processed. */
1067 rc = process_queued_samples(in);
1072 static int end(struct sr_input *in)
1074 struct context *inc;
1077 /* Nothing to do here if we never started feeding the session. */
1082 * Process sample data that may not have been forwarded before.
1083 * Flush any potentially queued samples.
1085 rc = process_queued_samples(in);
1088 rc = send_buffer(in);
1092 /* End the session feed if one was started. */
1094 if (inc->header_sent) {
1095 rc = std_session_send_df_end(in->sdi);
1096 inc->header_sent = FALSE;
1102 static void cleanup(struct sr_input *in)
1104 struct context *inc;
1115 * Release potentially allocated resources. Void all references
1116 * and scalars, so that re-runs start out fresh again.
1118 g_free(inc->sw_version);
1120 g_string_free(inc->cont_buff, TRUE);
1121 g_free(inc->sample_data_queue);
1122 for (idx = 0; idx < inc->channel_count; idx++)
1123 g_free(inc->wire_names[idx]);
1124 for (idx = 0; idx < inc->channel_count; idx++)
1125 g_free(inc->signal_names[idx]);
1126 g_slist_free_full(inc->signal_groups, sg_free);
1127 g_slist_free_full(inc->channels, g_free);
1128 g_free(inc->feed_buffer);
1129 memset(inc, 0, sizeof(*inc));
1132 static int reset(struct sr_input *in)
1134 struct context *inc;
1140 * The input module's .reset() routine clears the 'inc' context,
1141 * but 'in' is kept which contains channel groups which reference
1142 * channels. Since we cannot re-create the channels (applications
1143 * don't expect us to, see bug #1215), make sure to keep the
1144 * channels across the reset operation.
1146 channels = inc->channels;
1147 inc->channels = NULL;
1149 inc->channels = channels;
1154 static struct sr_option options[] = {
1158 static const struct sr_option *get_options(void)
1163 SR_PRIV struct sr_input_module input_logicport = {
1165 .name = "LogicPort File",
1166 .desc = "Intronix LA1034 LogicPort project",
1167 .exts = (const char *[]){ "lpf", NULL },
1168 .metadata = { SR_INPUT_META_HEADER | SR_INPUT_META_REQUIRED },
1169 .options = get_options,
1170 .format_match = format_match,