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)
33 #define CHRONOVU_LA8_FILESIZE ((8 * 1024 * 1024) + 5)
40 static int format_match(GHashTable *metadata, unsigned int *confidence)
45 * In the absence of a reliable condition like magic strings,
46 * we can only guess based on the file size. Since this is
47 * rather weak a condition, signal "little confidence" and
48 * optionally give precedence to better matches.
50 size = GPOINTER_TO_SIZE(g_hash_table_lookup(metadata,
51 GINT_TO_POINTER(SR_INPUT_META_FILESIZE)));
52 if (size != CHRONOVU_LA8_FILESIZE)
59 static int init(struct sr_input *in, GHashTable *options)
65 num_channels = g_variant_get_int32(g_hash_table_lookup(options, "numchannels"));
66 if (num_channels < 1) {
67 sr_err("Invalid value for numchannels: must be at least 1.");
71 in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
72 in->priv = inc = g_malloc0(sizeof(struct context));
74 inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
76 for (i = 0; i < num_channels; i++) {
77 snprintf(name, sizeof(name), "%d", i);
78 sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
84 static int process_buffer(struct sr_input *in)
86 struct sr_datafeed_packet packet;
87 struct sr_datafeed_meta meta;
88 struct sr_datafeed_logic logic;
89 struct sr_config *src;
96 std_session_send_df_header(in->sdi);
98 if (inc->samplerate) {
99 packet.type = SR_DF_META;
100 packet.payload = &meta;
101 src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate));
102 meta.config = g_slist_append(NULL, src);
103 sr_session_send(in->sdi, &packet);
104 g_slist_free(meta.config);
111 packet.type = SR_DF_LOGIC;
112 packet.payload = &logic;
113 logic.unitsize = (g_slist_length(in->sdi->channels) + 7) / 8;
115 /* Cut off at multiple of unitsize. */
116 chunk_size = in->buf->len / logic.unitsize * logic.unitsize;
118 for (i = 0; i < chunk_size; i += chunk) {
119 logic.data = in->buf->str + i;
120 chunk = MIN(CHUNK_SIZE, chunk_size - i);
121 logic.length = chunk;
122 sr_session_send(in->sdi, &packet);
124 g_string_erase(in->buf, 0, chunk_size);
129 static int receive(struct sr_input *in, GString *buf)
133 g_string_append_len(in->buf, buf->str, buf->len);
135 if (!in->sdi_ready) {
136 /* sdi is ready, notify frontend. */
137 in->sdi_ready = TRUE;
141 ret = process_buffer(in);
146 static int end(struct sr_input *in)
152 ret = process_buffer(in);
158 std_session_send_df_end(in->sdi);
163 static int reset(struct sr_input *in)
165 struct context *inc = in->priv;
167 inc->started = FALSE;
168 g_string_truncate(in->buf, 0);
173 static struct sr_option options[] = {
174 { "numchannels", "Number of logic channels", "The number of (logic) channels in the data", NULL, NULL },
175 { "samplerate", "Sample rate (Hz)", "The sample rate of the (logic) data in Hz", NULL, NULL },
179 static const struct sr_option *get_options(void)
181 if (!options[0].def) {
182 options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS));
183 options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE));
189 SR_PRIV struct sr_input_module input_chronovu_la8 = {
190 .id = "chronovu-la8",
191 .name = "ChronoVu LA8",
192 .desc = "ChronoVu LA8 native file format data",
193 .exts = (const char*[]){"kdt", NULL},
194 .metadata = { SR_INPUT_META_FILESIZE | SR_INPUT_META_REQUIRED },
195 .options = get_options,
196 .format_match = format_match,