]> sigrok.org Git - libsigrok.git/blame - src/input/chronovu_la8.c
input/chronovu_la8: only send data to the session, don't send the header
[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
02e24c0c
BV
50struct context {
51 gboolean started;
52 uint64_t samplerate;
e8eb2422 53 uint64_t samples_remain;
02e24c0c 54};
83e9d586 55
54ee427d 56static int format_match(GHashTable *metadata, unsigned int *confidence)
83e9d586 57{
f54a55da 58 uint64_t size;
83e9d586 59
54ee427d
GS
60 /*
61 * In the absence of a reliable condition like magic strings,
62 * we can only guess based on the file size. Since this is
63 * rather weak a condition, signal "little confidence" and
64 * optionally give precedence to better matches.
65 */
f54a55da 66 size = GPOINTER_TO_SIZE(g_hash_table_lookup(metadata,
02e24c0c 67 GINT_TO_POINTER(SR_INPUT_META_FILESIZE)));
54ee427d
GS
68 if (size != CHRONOVU_LA8_FILESIZE)
69 return SR_ERR;
70 *confidence = 100;
83e9d586 71
54ee427d 72 return SR_OK;
83e9d586
UH
73}
74
02e24c0c 75static int init(struct sr_input *in, GHashTable *options)
83e9d586 76{
02e24c0c 77 struct context *inc;
ba7dd8bb 78 int num_channels, i;
02e24c0c
BV
79 char name[16];
80
81 num_channels = g_variant_get_int32(g_hash_table_lookup(options, "numchannels"));
82 if (num_channels < 1) {
83 sr_err("Invalid value for numchannels: must be at least 1.");
84 return SR_ERR_ARG;
85 }
86
aac29cc1 87 in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
02e24c0c 88 in->priv = inc = g_malloc0(sizeof(struct context));
edd28877
BV
89
90 inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
464d12c7 91
ba7dd8bb 92 for (i = 0; i < num_channels; i++) {
00ed77f2 93 snprintf(name, sizeof(name), "%d", i);
5e23fcab 94 sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
464d12c7 95 }
83e9d586
UH
96
97 return SR_OK;
98}
99
7066fd46 100static int process_buffer(struct sr_input *in)
83e9d586 101{
83e9d586 102 struct sr_datafeed_packet packet;
2df1e819 103 struct sr_datafeed_meta meta;
9c939c51 104 struct sr_datafeed_logic logic;
2df1e819 105 struct sr_config *src;
02e24c0c
BV
106 struct context *inc;
107 gsize chunk_size, i;
e8eb2422
GS
108 gsize chunk;
109 uint16_t unitsize;
83e9d586 110
02e24c0c 111 inc = in->priv;
e8eb2422
GS
112 unitsize = (g_slist_length(in->sdi->channels) + 7) / 8;
113
04c2f202 114 if (!inc->started) {
bee2b016 115 std_session_send_df_header(in->sdi);
04c2f202 116
d0181813
BV
117 if (inc->samplerate) {
118 packet.type = SR_DF_META;
119 packet.payload = &meta;
120 src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate));
121 meta.config = g_slist_append(NULL, src);
122 sr_session_send(in->sdi, &packet);
c01378c9 123 g_slist_free(meta.config);
d0181813
BV
124 sr_config_free(src);
125 }
126
e8eb2422
GS
127 inc->samples_remain = CHRONOVU_LA8_DATASIZE;
128 inc->samples_remain /= unitsize;
129
d0181813 130 inc->started = TRUE;
04c2f202 131 }
83e9d586 132
83e9d586 133 packet.type = SR_DF_LOGIC;
9c939c51 134 packet.payload = &logic;
e8eb2422 135 logic.unitsize = unitsize;
02e24c0c 136
e8eb2422 137 /* Cut off at multiple of unitsize. Avoid sending the "header". */
02e24c0c 138 chunk_size = in->buf->len / logic.unitsize * logic.unitsize;
e8eb2422 139 chunk_size = MIN(chunk_size, inc->samples_remain * unitsize);
83e9d586 140
02e24c0c 141 for (i = 0; i < chunk_size; i += chunk) {
04c2f202 142 logic.data = in->buf->str + i;
8bc2fa6d 143 chunk = MIN(CHUNK_SIZE, chunk_size - i);
e8eb2422
GS
144 if (chunk) {
145 logic.length = chunk;
146 sr_session_send(in->sdi, &packet);
147 inc->samples_remain -= chunk / unitsize;
148 }
83e9d586 149 }
04c2f202 150 g_string_erase(in->buf, 0, chunk_size);
02e24c0c
BV
151
152 return SR_OK;
153}
154
7066fd46
BV
155static int receive(struct sr_input *in, GString *buf)
156{
157 int ret;
158
159 g_string_append_len(in->buf, buf->str, buf->len);
160
161 if (!in->sdi_ready) {
162 /* sdi is ready, notify frontend. */
163 in->sdi_ready = TRUE;
164 return SR_OK;
165 }
166
167 ret = process_buffer(in);
168
169 return ret;
170}
171
172static int end(struct sr_input *in)
02e24c0c 173{
02e24c0c 174 struct context *inc;
7066fd46 175 int ret;
02e24c0c 176
7066fd46
BV
177 if (in->sdi_ready)
178 ret = process_buffer(in);
179 else
180 ret = SR_OK;
02e24c0c 181
7066fd46 182 inc = in->priv;
3be42bc2 183 if (inc->started)
bee2b016 184 std_session_send_df_end(in->sdi);
83e9d586 185
7066fd46 186 return ret;
83e9d586
UH
187}
188
ef9d3fef
SA
189static int reset(struct sr_input *in)
190{
191 struct context *inc = in->priv;
192
193 inc->started = FALSE;
194 g_string_truncate(in->buf, 0);
195
196 return SR_OK;
197}
198
02e24c0c 199static struct sr_option options[] = {
867293a1
UH
200 { "numchannels", "Number of logic channels", "The number of (logic) channels in the data", NULL, NULL },
201 { "samplerate", "Sample rate (Hz)", "The sample rate of the (logic) data in Hz", NULL, NULL },
06ad20be 202 ALL_ZERO
02e24c0c
BV
203};
204
2c240774 205static const struct sr_option *get_options(void)
02e24c0c
BV
206{
207 if (!options[0].def) {
208 options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS));
edd28877 209 options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE));
02e24c0c
BV
210 }
211
212 return options;
213}
214
d4c93774 215SR_PRIV struct sr_input_module input_chronovu_la8 = {
83e9d586 216 .id = "chronovu-la8",
b20eb520
UH
217 .name = "ChronoVu LA8",
218 .desc = "ChronoVu LA8 native file format data",
c7bc82ff 219 .exts = (const char*[]){"kdt", NULL},
02e24c0c
BV
220 .metadata = { SR_INPUT_META_FILESIZE | SR_INPUT_META_REQUIRED },
221 .options = get_options,
83e9d586
UH
222 .format_match = format_match,
223 .init = init,
02e24c0c 224 .receive = receive,
7066fd46 225 .end = end,
ef9d3fef 226 .reset = reset,
83e9d586 227};