]> sigrok.org Git - libsigrok.git/blame_incremental - src/input/chronovu_la8.c
input/csv: style nits, drop @brief and DIAG in debug output
[libsigrok.git] / src / input / chronovu_la8.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2011 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 <stdlib.h>
22#include <fcntl.h>
23#include <unistd.h>
24#include <sys/stat.h>
25#include <libsigrok/libsigrok.h>
26#include "libsigrok-internal.h"
27
28#define LOG_PREFIX "input/chronovu-la8"
29
30#define DEFAULT_NUM_CHANNELS 8
31#define DEFAULT_SAMPLERATE SR_MHZ(100)
32#define CHUNK_SIZE (4 * 1024 * 1024)
33
34/*
35 * File layout:
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
42 * 200MHz for LA16.
43 * - Four bytes for the trigger position. This 32bit value is the
44 * sample number in little endian format, or 0 when unused.
45 */
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)
49
50/*
51 * Implementation note:
52 *
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.
59 *
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.
65 *
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).
78 */
79
80struct context {
81 gboolean started;
82 uint64_t samplerate;
83 uint64_t samples_remain;
84};
85
86static int format_match(GHashTable *metadata, unsigned int *confidence)
87{
88 uint64_t size;
89
90 /*
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.
95 */
96 size = GPOINTER_TO_SIZE(g_hash_table_lookup(metadata,
97 GINT_TO_POINTER(SR_INPUT_META_FILESIZE)));
98 if (size != CHRONOVU_LA8_FILESIZE)
99 return SR_ERR;
100 *confidence = 100;
101
102 return SR_OK;
103}
104
105static int init(struct sr_input *in, GHashTable *options)
106{
107 struct context *inc;
108 int num_channels, i;
109 char name[16];
110
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.");
114 return SR_ERR_ARG;
115 }
116
117 in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
118 in->priv = inc = g_malloc0(sizeof(struct context));
119
120 inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
121
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);
125 }
126
127 return SR_OK;
128}
129
130static int process_buffer(struct sr_input *in)
131{
132 struct sr_datafeed_packet packet;
133 struct sr_datafeed_meta meta;
134 struct sr_datafeed_logic logic;
135 struct sr_config *src;
136 struct context *inc;
137 gsize chunk_size, i;
138 gsize chunk;
139 uint16_t unitsize;
140
141 inc = in->priv;
142 unitsize = (g_slist_length(in->sdi->channels) + 7) / 8;
143
144 if (!inc->started) {
145 std_session_send_df_header(in->sdi);
146
147 if (inc->samplerate) {
148 packet.type = SR_DF_META;
149 packet.payload = &meta;
150 src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate));
151 meta.config = g_slist_append(NULL, src);
152 sr_session_send(in->sdi, &packet);
153 g_slist_free(meta.config);
154 sr_config_free(src);
155 }
156
157 inc->samples_remain = CHRONOVU_LA8_DATASIZE;
158 inc->samples_remain /= unitsize;
159
160 inc->started = TRUE;
161 }
162
163 packet.type = SR_DF_LOGIC;
164 packet.payload = &logic;
165 logic.unitsize = unitsize;
166
167 /* Cut off at multiple of unitsize. Avoid sending the "header". */
168 chunk_size = in->buf->len / logic.unitsize * logic.unitsize;
169 chunk_size = MIN(chunk_size, inc->samples_remain * unitsize);
170
171 for (i = 0; i < chunk_size; i += chunk) {
172 logic.data = in->buf->str + i;
173 chunk = MIN(CHUNK_SIZE, chunk_size - i);
174 if (chunk) {
175 logic.length = chunk;
176 sr_session_send(in->sdi, &packet);
177 inc->samples_remain -= chunk / unitsize;
178 }
179 }
180 g_string_erase(in->buf, 0, chunk_size);
181
182 return SR_OK;
183}
184
185static int receive(struct sr_input *in, GString *buf)
186{
187 int ret;
188
189 g_string_append_len(in->buf, buf->str, buf->len);
190
191 if (!in->sdi_ready) {
192 /* sdi is ready, notify frontend. */
193 in->sdi_ready = TRUE;
194 return SR_OK;
195 }
196
197 ret = process_buffer(in);
198
199 return ret;
200}
201
202static int end(struct sr_input *in)
203{
204 struct context *inc;
205 int ret;
206
207 if (in->sdi_ready)
208 ret = process_buffer(in);
209 else
210 ret = SR_OK;
211
212 inc = in->priv;
213 if (inc->started)
214 std_session_send_df_end(in->sdi);
215
216 return ret;
217}
218
219static int reset(struct sr_input *in)
220{
221 struct context *inc = in->priv;
222
223 inc->started = FALSE;
224 g_string_truncate(in->buf, 0);
225
226 return SR_OK;
227}
228
229static struct sr_option options[] = {
230 { "numchannels", "Number of logic channels", "The number of (logic) channels in the data", NULL, NULL },
231 { "samplerate", "Sample rate (Hz)", "The sample rate of the (logic) data in Hz", NULL, NULL },
232 ALL_ZERO
233};
234
235static const struct sr_option *get_options(void)
236{
237 if (!options[0].def) {
238 options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS));
239 options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE));
240 }
241
242 return options;
243}
244
245SR_PRIV struct sr_input_module input_chronovu_la8 = {
246 .id = "chronovu-la8",
247 .name = "ChronoVu LA8/LA16",
248 .desc = "ChronoVu LA8/LA16 native file format data",
249 .exts = (const char*[]){"kdt", "kd1", NULL},
250 .metadata = { SR_INPUT_META_FILESIZE | SR_INPUT_META_REQUIRED },
251 .options = get_options,
252 .format_match = format_match,
253 .init = init,
254 .receive = receive,
255 .end = end,
256 .reset = reset,
257};