]> sigrok.org Git - libsigrok.git/blame - src/input/chronovu_la8.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / input / chronovu_la8.c
CommitLineData
83e9d586 1/*
50985c20 2 * This file is part of the libsigrok project.
83e9d586
UH
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
2ea1fdf1 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
83e9d586
UH
18 */
19
6ec6c43b 20#include <config.h>
83e9d586
UH
21#include <stdlib.h>
22#include <fcntl.h>
23#include <unistd.h>
36423d04 24#include <sys/stat.h>
c1aae900 25#include <libsigrok/libsigrok.h>
45c59c8b 26#include "libsigrok-internal.h"
83e9d586 27
b95dd761 28#define LOG_PREFIX "input/chronovu-la8"
8e7f1cfd 29
02e24c0c 30#define DEFAULT_NUM_CHANNELS 8
1a46cc62 31#define DEFAULT_SAMPLERATE SR_MHZ(100)
9a4fd01a 32#define CHUNK_SIZE (4 * 1024 * 1024)
e8eb2422
GS
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)
83e9d586 49
13ac0927
GS
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
02e24c0c
BV
80struct context {
81 gboolean started;
82 uint64_t samplerate;
e8eb2422 83 uint64_t samples_remain;
02e24c0c 84};
83e9d586 85
54ee427d 86static int format_match(GHashTable *metadata, unsigned int *confidence)
83e9d586 87{
f54a55da 88 uint64_t size;
83e9d586 89
54ee427d
GS
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 */
f54a55da 96 size = GPOINTER_TO_SIZE(g_hash_table_lookup(metadata,
02e24c0c 97 GINT_TO_POINTER(SR_INPUT_META_FILESIZE)));
54ee427d
GS
98 if (size != CHRONOVU_LA8_FILESIZE)
99 return SR_ERR;
100 *confidence = 100;
83e9d586 101
54ee427d 102 return SR_OK;
83e9d586
UH
103}
104
02e24c0c 105static int init(struct sr_input *in, GHashTable *options)
83e9d586 106{
02e24c0c 107 struct context *inc;
ba7dd8bb 108 int num_channels, i;
02e24c0c
BV
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
aac29cc1 117 in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
02e24c0c 118 in->priv = inc = g_malloc0(sizeof(struct context));
edd28877
BV
119
120 inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
464d12c7 121
ba7dd8bb 122 for (i = 0; i < num_channels; i++) {
00ed77f2 123 snprintf(name, sizeof(name), "%d", i);
5e23fcab 124 sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
464d12c7 125 }
83e9d586
UH
126
127 return SR_OK;
128}
129
7066fd46 130static int process_buffer(struct sr_input *in)
83e9d586 131{
83e9d586 132 struct sr_datafeed_packet packet;
9c939c51 133 struct sr_datafeed_logic logic;
02e24c0c
BV
134 struct context *inc;
135 gsize chunk_size, i;
e8eb2422
GS
136 gsize chunk;
137 uint16_t unitsize;
83e9d586 138
02e24c0c 139 inc = in->priv;
e8eb2422
GS
140 unitsize = (g_slist_length(in->sdi->channels) + 7) / 8;
141
04c2f202 142 if (!inc->started) {
bee2b016 143 std_session_send_df_header(in->sdi);
04c2f202 144
d0181813 145 if (inc->samplerate) {
f8a8d4bb
GS
146 (void)sr_session_send_meta(in->sdi, SR_CONF_SAMPLERATE,
147 g_variant_new_uint64(inc->samplerate));
d0181813
BV
148 }
149
e8eb2422
GS
150 inc->samples_remain = CHRONOVU_LA8_DATASIZE;
151 inc->samples_remain /= unitsize;
152
d0181813 153 inc->started = TRUE;
04c2f202 154 }
83e9d586 155
83e9d586 156 packet.type = SR_DF_LOGIC;
9c939c51 157 packet.payload = &logic;
e8eb2422 158 logic.unitsize = unitsize;
02e24c0c 159
e8eb2422 160 /* Cut off at multiple of unitsize. Avoid sending the "header". */
02e24c0c 161 chunk_size = in->buf->len / logic.unitsize * logic.unitsize;
e8eb2422 162 chunk_size = MIN(chunk_size, inc->samples_remain * unitsize);
83e9d586 163
02e24c0c 164 for (i = 0; i < chunk_size; i += chunk) {
04c2f202 165 logic.data = in->buf->str + i;
8bc2fa6d 166 chunk = MIN(CHUNK_SIZE, chunk_size - i);
e8eb2422
GS
167 if (chunk) {
168 logic.length = chunk;
169 sr_session_send(in->sdi, &packet);
170 inc->samples_remain -= chunk / unitsize;
171 }
83e9d586 172 }
04c2f202 173 g_string_erase(in->buf, 0, chunk_size);
02e24c0c
BV
174
175 return SR_OK;
176}
177
7066fd46
BV
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)
02e24c0c 196{
02e24c0c 197 struct context *inc;
7066fd46 198 int ret;
02e24c0c 199
7066fd46
BV
200 if (in->sdi_ready)
201 ret = process_buffer(in);
202 else
203 ret = SR_OK;
02e24c0c 204
7066fd46 205 inc = in->priv;
3be42bc2 206 if (inc->started)
bee2b016 207 std_session_send_df_end(in->sdi);
83e9d586 208
7066fd46 209 return ret;
83e9d586
UH
210}
211
ef9d3fef
SA
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
02e24c0c 222static struct sr_option options[] = {
867293a1
UH
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 },
06ad20be 225 ALL_ZERO
02e24c0c
BV
226};
227
2c240774 228static const struct sr_option *get_options(void)
02e24c0c
BV
229{
230 if (!options[0].def) {
231 options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS));
edd28877 232 options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE));
02e24c0c
BV
233 }
234
235 return options;
236}
237
d4c93774 238SR_PRIV struct sr_input_module input_chronovu_la8 = {
83e9d586 239 .id = "chronovu-la8",
13ac0927
GS
240 .name = "ChronoVu LA8/LA16",
241 .desc = "ChronoVu LA8/LA16 native file format data",
242 .exts = (const char*[]){"kdt", "kd1", NULL},
02e24c0c
BV
243 .metadata = { SR_INPUT_META_FILESIZE | SR_INPUT_META_REQUIRED },
244 .options = get_options,
83e9d586
UH
245 .format_match = format_match,
246 .init = init,
02e24c0c 247 .receive = receive,
7066fd46 248 .end = end,
ef9d3fef 249 .reset = reset,
83e9d586 250};