]>
Commit | Line | Data |
---|---|---|
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) |
8cd15dd4 UH |
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 | */ | |
83e9d586 | 79 | |
02e24c0c BV |
80 | struct context { |
81 | gboolean started; | |
82 | uint64_t samplerate; | |
8cd15dd4 | 83 | uint64_t samples_remain; |
02e24c0c | 84 | }; |
83e9d586 | 85 | |
8cd15dd4 | 86 | static int format_match(GHashTable *metadata, unsigned int *confidence) |
83e9d586 | 87 | { |
8cd15dd4 UH |
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, | |
02e24c0c | 97 | GINT_TO_POINTER(SR_INPUT_META_FILESIZE))); |
8cd15dd4 UH |
98 | if (size != CHRONOVU_LA8_FILESIZE) |
99 | return SR_ERR; | |
100 | *confidence = 100; | |
83e9d586 | 101 | |
8cd15dd4 | 102 | return SR_OK; |
83e9d586 UH |
103 | } |
104 | ||
02e24c0c | 105 | static 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++) { |
8cd15dd4 | 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 | 130 | static int process_buffer(struct sr_input *in) |
83e9d586 | 131 | { |
83e9d586 | 132 | struct sr_datafeed_packet packet; |
2df1e819 | 133 | struct sr_datafeed_meta meta; |
9c939c51 | 134 | struct sr_datafeed_logic logic; |
2df1e819 | 135 | struct sr_config *src; |
02e24c0c BV |
136 | struct context *inc; |
137 | gsize chunk_size, i; | |
8cd15dd4 UH |
138 | gsize chunk; |
139 | uint16_t unitsize; | |
83e9d586 | 140 | |
02e24c0c | 141 | inc = in->priv; |
8cd15dd4 UH |
142 | unitsize = (g_slist_length(in->sdi->channels) + 7) / 8; |
143 | ||
04c2f202 | 144 | if (!inc->started) { |
bee2b016 | 145 | std_session_send_df_header(in->sdi); |
04c2f202 | 146 | |
d0181813 BV |
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); | |
c01378c9 | 153 | g_slist_free(meta.config); |
d0181813 BV |
154 | sr_config_free(src); |
155 | } | |
156 | ||
8cd15dd4 UH |
157 | inc->samples_remain = CHRONOVU_LA8_DATASIZE; |
158 | inc->samples_remain /= unitsize; | |
159 | ||
d0181813 | 160 | inc->started = TRUE; |
04c2f202 | 161 | } |
83e9d586 | 162 | |
83e9d586 | 163 | packet.type = SR_DF_LOGIC; |
9c939c51 | 164 | packet.payload = &logic; |
8cd15dd4 | 165 | logic.unitsize = unitsize; |
02e24c0c | 166 | |
8cd15dd4 | 167 | /* Cut off at multiple of unitsize. Avoid sending the "header". */ |
02e24c0c | 168 | chunk_size = in->buf->len / logic.unitsize * logic.unitsize; |
8cd15dd4 | 169 | chunk_size = MIN(chunk_size, inc->samples_remain * unitsize); |
83e9d586 | 170 | |
02e24c0c | 171 | for (i = 0; i < chunk_size; i += chunk) { |
04c2f202 | 172 | logic.data = in->buf->str + i; |
8cd15dd4 UH |
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 | } | |
83e9d586 | 179 | } |
04c2f202 | 180 | g_string_erase(in->buf, 0, chunk_size); |
02e24c0c BV |
181 | |
182 | return SR_OK; | |
183 | } | |
184 | ||
7066fd46 BV |
185 | static 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 | ||
202 | static int end(struct sr_input *in) | |
02e24c0c | 203 | { |
02e24c0c | 204 | struct context *inc; |
7066fd46 | 205 | int ret; |
02e24c0c | 206 | |
7066fd46 BV |
207 | if (in->sdi_ready) |
208 | ret = process_buffer(in); | |
209 | else | |
210 | ret = SR_OK; | |
02e24c0c | 211 | |
7066fd46 | 212 | inc = in->priv; |
3be42bc2 | 213 | if (inc->started) |
bee2b016 | 214 | std_session_send_df_end(in->sdi); |
83e9d586 | 215 | |
7066fd46 | 216 | return ret; |
83e9d586 UH |
217 | } |
218 | ||
ef9d3fef SA |
219 | static 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 | ||
02e24c0c | 229 | static struct sr_option options[] = { |
8cd15dd4 UH |
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 }, | |
06ad20be | 232 | ALL_ZERO |
02e24c0c BV |
233 | }; |
234 | ||
2c240774 | 235 | static const struct sr_option *get_options(void) |
02e24c0c BV |
236 | { |
237 | if (!options[0].def) { | |
238 | options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS)); | |
edd28877 | 239 | options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE)); |
02e24c0c BV |
240 | } |
241 | ||
242 | return options; | |
243 | } | |
244 | ||
d4c93774 | 245 | SR_PRIV struct sr_input_module input_chronovu_la8 = { |
83e9d586 | 246 | .id = "chronovu-la8", |
8cd15dd4 UH |
247 | .name = "ChronoVu LA8/LA16", |
248 | .desc = "ChronoVu LA8/LA16 native file format data", | |
249 | .exts = (const char*[]){"kdt", "kd1", NULL}, | |
02e24c0c BV |
250 | .metadata = { SR_INPUT_META_FILESIZE | SR_INPUT_META_REQUIRED }, |
251 | .options = get_options, | |
83e9d586 UH |
252 | .format_match = format_match, |
253 | .init = init, | |
02e24c0c | 254 | .receive = receive, |
7066fd46 | 255 | .end = end, |
ef9d3fef | 256 | .reset = reset, |
83e9d586 | 257 | }; |