]> sigrok.org Git - libsigrok.git/blame_incremental - src/input/chronovu_la8.c
output/csv: use intermediate time_t var, silence compiler warning
[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_logic logic;
134 struct context *inc;
135 gsize chunk_size, i;
136 gsize chunk;
137 uint16_t unitsize;
138
139 inc = in->priv;
140 unitsize = (g_slist_length(in->sdi->channels) + 7) / 8;
141
142 if (!inc->started) {
143 std_session_send_df_header(in->sdi);
144
145 if (inc->samplerate) {
146 (void)sr_session_send_meta(in->sdi, SR_CONF_SAMPLERATE,
147 g_variant_new_uint64(inc->samplerate));
148 }
149
150 inc->samples_remain = CHRONOVU_LA8_DATASIZE;
151 inc->samples_remain /= unitsize;
152
153 inc->started = TRUE;
154 }
155
156 packet.type = SR_DF_LOGIC;
157 packet.payload = &logic;
158 logic.unitsize = unitsize;
159
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);
163
164 for (i = 0; i < chunk_size; i += chunk) {
165 logic.data = in->buf->str + i;
166 chunk = MIN(CHUNK_SIZE, chunk_size - i);
167 if (chunk) {
168 logic.length = chunk;
169 sr_session_send(in->sdi, &packet);
170 inc->samples_remain -= chunk / unitsize;
171 }
172 }
173 g_string_erase(in->buf, 0, chunk_size);
174
175 return SR_OK;
176}
177
178static int receive(struct sr_input *in, GString *buf)
179{
180 int ret;
181
182 g_string_append_len(in->buf, buf->str, buf->len);
183
184 if (!in->sdi_ready) {
185 /* sdi is ready, notify frontend. */
186 in->sdi_ready = TRUE;
187 return SR_OK;
188 }
189
190 ret = process_buffer(in);
191
192 return ret;
193}
194
195static int end(struct sr_input *in)
196{
197 struct context *inc;
198 int ret;
199
200 if (in->sdi_ready)
201 ret = process_buffer(in);
202 else
203 ret = SR_OK;
204
205 inc = in->priv;
206 if (inc->started)
207 std_session_send_df_end(in->sdi);
208
209 return ret;
210}
211
212static int reset(struct sr_input *in)
213{
214 struct context *inc = in->priv;
215
216 inc->started = FALSE;
217 g_string_truncate(in->buf, 0);
218
219 return SR_OK;
220}
221
222static 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 },
225 ALL_ZERO
226};
227
228static const struct sr_option *get_options(void)
229{
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));
233 }
234
235 return options;
236}
237
238SR_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,
246 .init = init,
247 .receive = receive,
248 .end = end,
249 .reset = reset,
250};