]> sigrok.org Git - libsigrok.git/blob - 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
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 struct context {
51         gboolean started;
52         uint64_t samplerate;
53         uint64_t samples_remain;
54 };
55
56 static int format_match(GHashTable *metadata, unsigned int *confidence)
57 {
58         uint64_t size;
59
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          */
66         size = GPOINTER_TO_SIZE(g_hash_table_lookup(metadata,
67                         GINT_TO_POINTER(SR_INPUT_META_FILESIZE)));
68         if (size != CHRONOVU_LA8_FILESIZE)
69                 return SR_ERR;
70         *confidence = 100;
71
72         return SR_OK;
73 }
74
75 static int init(struct sr_input *in, GHashTable *options)
76 {
77         struct context *inc;
78         int num_channels, i;
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
87         in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
88         in->priv = inc = g_malloc0(sizeof(struct context));
89
90         inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
91
92         for (i = 0; i < num_channels; i++) {
93                 snprintf(name, sizeof(name), "%d", i);
94                 sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
95         }
96
97         return SR_OK;
98 }
99
100 static int process_buffer(struct sr_input *in)
101 {
102         struct sr_datafeed_packet packet;
103         struct sr_datafeed_meta meta;
104         struct sr_datafeed_logic logic;
105         struct sr_config *src;
106         struct context *inc;
107         gsize chunk_size, i;
108         gsize chunk;
109         uint16_t unitsize;
110
111         inc = in->priv;
112         unitsize = (g_slist_length(in->sdi->channels) + 7) / 8;
113
114         if (!inc->started) {
115                 std_session_send_df_header(in->sdi);
116
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);
123                         g_slist_free(meta.config);
124                         sr_config_free(src);
125                 }
126
127                 inc->samples_remain = CHRONOVU_LA8_DATASIZE;
128                 inc->samples_remain /= unitsize;
129
130                 inc->started = TRUE;
131         }
132
133         packet.type = SR_DF_LOGIC;
134         packet.payload = &logic;
135         logic.unitsize = unitsize;
136
137         /* Cut off at multiple of unitsize. Avoid sending the "header". */
138         chunk_size = in->buf->len / logic.unitsize * logic.unitsize;
139         chunk_size = MIN(chunk_size, inc->samples_remain * unitsize);
140
141         for (i = 0; i < chunk_size; i += chunk) {
142                 logic.data = in->buf->str + i;
143                 chunk = MIN(CHUNK_SIZE, chunk_size - i);
144                 if (chunk) {
145                         logic.length = chunk;
146                         sr_session_send(in->sdi, &packet);
147                         inc->samples_remain -= chunk / unitsize;
148                 }
149         }
150         g_string_erase(in->buf, 0, chunk_size);
151
152         return SR_OK;
153 }
154
155 static 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
172 static int end(struct sr_input *in)
173 {
174         struct context *inc;
175         int ret;
176
177         if (in->sdi_ready)
178                 ret = process_buffer(in);
179         else
180                 ret = SR_OK;
181
182         inc = in->priv;
183         if (inc->started)
184                 std_session_send_df_end(in->sdi);
185
186         return ret;
187 }
188
189 static 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
199 static struct sr_option options[] = {
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 },
202         ALL_ZERO
203 };
204
205 static const struct sr_option *get_options(void)
206 {
207         if (!options[0].def) {
208                 options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS));
209                 options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE));
210         }
211
212         return options;
213 }
214
215 SR_PRIV struct sr_input_module input_chronovu_la8 = {
216         .id = "chronovu-la8",
217         .name = "ChronoVu LA8",
218         .desc = "ChronoVu LA8 native file format data",
219         .exts = (const char*[]){"kdt", NULL},
220         .metadata = { SR_INPUT_META_FILESIZE | SR_INPUT_META_REQUIRED },
221         .options = get_options,
222         .format_match = format_match,
223         .init = init,
224         .receive = receive,
225         .end = end,
226         .reset = reset,
227 };