2 * This file is part of the libsigrok project.
4 * Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
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/>.
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
28 #define LOG_PREFIX "input/chronovu-la8"
30 #define DEFAULT_NUM_CHANNELS 8
31 #define DEFAULT_SAMPLERATE SR_MHZ(100)
32 #define CHUNK_SIZE (4 * 1024 * 1024)
36 * - Fixed size 8MiB data part at offset 0.
37 * - Either one byte per sample for LA8.
38 * - Or two bytes per sample for LA16, in little endian format.
39 * - Five byte "header" at offset 8MiB.
40 * - One "clock divider" byte. The byte value is the divider factor
41 * minus 1. Value 0xff is invalid. Base clock is 100MHz for LA8, or
43 * - Four bytes for the trigger position. This 32bit value is the
44 * sample number in little endian format, or 0 when unused.
46 #define CHRONOVU_LA8_DATASIZE (8 * 1024 * 1024)
47 #define CHRONOVU_LA8_HDRSIZE (sizeof(uint8_t) + sizeof(uint32_t))
48 #define CHRONOVU_LA8_FILESIZE (CHRONOVU_LA8_DATASIZE + CHRONOVU_LA8_HDRSIZE)
51 * Implementation note:
53 * The .format_match() routine only checks the file size, but none of
54 * the header fields. Only little would be gained (only clock divider
55 * 0xff could get tested), but complexity would increase dramatically.
56 * Also the .format_match() routine is unlikely to receive large enough
57 * a buffer to include the header. Neither is the filename available to
58 * the .format_match() routine.
60 * There is no way to programmatically tell whether the file was created
61 * by LA8 or LA16 software, i.e. with 8 or 16 logic channels. If the
62 * filename was available, one might guess based on the file extension,
63 * but still would require user specs if neither of the known extensions
64 * were used or the input is fed from a pipe.
66 * The current input module implementation assumes that users specify
67 * the (channel count and) sample rate. Input data gets processed and
68 * passed along to the session bus, before the file "header" is seen.
69 * A future implementation could move channel creation from init() to
70 * receive() or end() (actually: a common routine called from those two
71 * routines), and could defer sample processing and feeding the session
72 * until the header was seen, including deferred samplerate calculation
73 * after having seen the header. But again this improvement depends on
74 * the availability of either the filename or the device type. Also note
75 * that applications then had to keep sending data to the input module's
76 * receive() routine until sufficient amounts of input data were seen
77 * including the header (see bug #1017).
83 uint64_t samples_remain;
86 static int format_match(GHashTable *metadata, unsigned int *confidence)
91 * In the absence of a reliable condition like magic strings,
92 * we can only guess based on the file size. Since this is
93 * rather weak a condition, signal "little confidence" and
94 * optionally give precedence to better matches.
96 size = GPOINTER_TO_SIZE(g_hash_table_lookup(metadata,
97 GINT_TO_POINTER(SR_INPUT_META_FILESIZE)));
98 if (size != CHRONOVU_LA8_FILESIZE)
105 static int init(struct sr_input *in, GHashTable *options)
111 num_channels = g_variant_get_int32(g_hash_table_lookup(options, "numchannels"));
112 if (num_channels < 1) {
113 sr_err("Invalid value for numchannels: must be at least 1.");
117 in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
118 in->priv = inc = g_malloc0(sizeof(struct context));
120 inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
122 for (i = 0; i < num_channels; i++) {
123 snprintf(name, sizeof(name), "%d", i);
124 sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
130 static int process_buffer(struct sr_input *in)
132 struct sr_datafeed_packet packet;
133 struct sr_datafeed_logic logic;
140 unitsize = (g_slist_length(in->sdi->channels) + 7) / 8;
143 std_session_send_df_header(in->sdi);
145 if (inc->samplerate) {
146 (void)sr_session_send_meta(in->sdi, SR_CONF_SAMPLERATE,
147 g_variant_new_uint64(inc->samplerate));
150 inc->samples_remain = CHRONOVU_LA8_DATASIZE;
151 inc->samples_remain /= unitsize;
156 packet.type = SR_DF_LOGIC;
157 packet.payload = &logic;
158 logic.unitsize = unitsize;
160 /* Cut off at multiple of unitsize. Avoid sending the "header". */
161 chunk_size = in->buf->len / logic.unitsize * logic.unitsize;
162 chunk_size = MIN(chunk_size, inc->samples_remain * unitsize);
164 for (i = 0; i < chunk_size; i += chunk) {
165 logic.data = in->buf->str + i;
166 chunk = MIN(CHUNK_SIZE, chunk_size - i);
168 logic.length = chunk;
169 sr_session_send(in->sdi, &packet);
170 inc->samples_remain -= chunk / unitsize;
173 g_string_erase(in->buf, 0, chunk_size);
178 static int receive(struct sr_input *in, GString *buf)
182 g_string_append_len(in->buf, buf->str, buf->len);
184 if (!in->sdi_ready) {
185 /* sdi is ready, notify frontend. */
186 in->sdi_ready = TRUE;
190 ret = process_buffer(in);
195 static int end(struct sr_input *in)
201 ret = process_buffer(in);
207 std_session_send_df_end(in->sdi);
212 static int reset(struct sr_input *in)
214 struct context *inc = in->priv;
216 inc->started = FALSE;
217 g_string_truncate(in->buf, 0);
222 static struct sr_option options[] = {
223 { "numchannels", "Number of logic channels", "The number of (logic) channels in the data", NULL, NULL },
224 { "samplerate", "Sample rate (Hz)", "The sample rate of the (logic) data in Hz", NULL, NULL },
228 static const struct sr_option *get_options(void)
230 if (!options[0].def) {
231 options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS));
232 options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE));
238 SR_PRIV struct sr_input_module input_chronovu_la8 = {
239 .id = "chronovu-la8",
240 .name = "ChronoVu LA8/LA16",
241 .desc = "ChronoVu LA8/LA16 native file format data",
242 .exts = (const char*[]){"kdt", "kd1", NULL},
243 .metadata = { SR_INPUT_META_FILESIZE | SR_INPUT_META_REQUIRED },
244 .options = get_options,
245 .format_match = format_match,