]>
Commit | Line | Data |
---|---|---|
e6b15cb5 SB |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
5 | * Copyright (C) 2015 Stefan Brüns <stefan.bruens@rwth-aachen.de> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <config.h> | |
22 | #include <sys/types.h> | |
23 | #include <sys/stat.h> | |
24 | #include <unistd.h> | |
25 | #include <ctype.h> | |
26 | #include <string.h> | |
27 | #include <stdint.h> | |
28 | #include <libsigrok/libsigrok.h> | |
29 | #include "libsigrok-internal.h" | |
30 | ||
31 | #define LOG_PREFIX "input/raw_analog" | |
32 | ||
33 | /* How many bytes at a time to process and send to the session bus. */ | |
9a4fd01a | 34 | #define CHUNK_SIZE (4 * 1024 * 1024) |
d9251a2c UH |
35 | #define DEFAULT_NUM_CHANNELS 1 |
36 | #define DEFAULT_SAMPLERATE 0 | |
e6b15cb5 SB |
37 | |
38 | struct context { | |
39 | gboolean started; | |
40 | int fmt_index; | |
41 | uint64_t samplerate; | |
42 | int samplesize; | |
43 | struct sr_datafeed_packet packet; | |
44 | struct sr_datafeed_analog analog; | |
45 | struct sr_analog_encoding encoding; | |
46 | struct sr_analog_meaning meaning; | |
47 | struct sr_analog_spec spec; | |
48 | }; | |
49 | ||
50 | struct sample_format { | |
1393bccf | 51 | const char *fmt_name; |
221cec31 | 52 | struct sr_analog_encoding encoding; |
e6b15cb5 SB |
53 | }; |
54 | ||
1393bccf | 55 | static const struct sample_format sample_formats[] = |
e6b15cb5 | 56 | { |
30726a8a UH |
57 | { "S8", { 1, TRUE, FALSE, FALSE, 0, TRUE, { 1, 128}, { 0, 1}}}, |
58 | { "U8", { 1, FALSE, FALSE, FALSE, 0, TRUE, { 1, 255}, {-1, 2}}}, | |
59 | { "S16_LE", { 2, TRUE, FALSE, FALSE, 0, TRUE, { 1, INT16_MAX + 1}, { 0, 1}}}, | |
60 | { "U16_LE", { 2, FALSE, FALSE, FALSE, 0, TRUE, { 1, UINT16_MAX}, {-1, 2}}}, | |
61 | { "S16_BE", { 2, TRUE, FALSE, TRUE, 0, TRUE, { 1, INT16_MAX + 1}, { 0, 1}}}, | |
62 | { "U16_BE", { 2, FALSE, FALSE, TRUE, 0, TRUE, { 1, UINT16_MAX}, {-1, 2}}}, | |
63 | { "S32_LE", { 4, TRUE, FALSE, FALSE, 0, TRUE, { 1, (uint64_t)INT32_MAX + 1}, { 0, 1}}}, | |
64 | { "U32_LE", { 4, FALSE, FALSE, FALSE, 0, TRUE, { 1, UINT32_MAX}, {-1, 2}}}, | |
65 | { "S32_BE", { 4, TRUE, FALSE, TRUE, 0, TRUE, { 1, (uint64_t)INT32_MAX + 1}, { 0, 1}}}, | |
66 | { "U32_BE", { 4, FALSE, FALSE, TRUE, 0, TRUE, { 1, UINT32_MAX}, {-1, 2}}}, | |
67 | { "FLOAT_LE", { 4, TRUE, TRUE, FALSE, 0, TRUE, { 1, 1}, { 0, 1}}}, | |
68 | { "FLOAT_BE", { 4, TRUE, TRUE, TRUE, 0, TRUE, { 1, 1}, { 0, 1}}}, | |
69 | { "FLOAT64_LE", { 8, TRUE, TRUE, FALSE, 0, TRUE, { 1, 1}, { 0, 1}}}, | |
70 | { "FLOAT64_BE", { 8, TRUE, TRUE, TRUE, 0, TRUE, { 1, 1}, { 0, 1}}}, | |
e6b15cb5 SB |
71 | }; |
72 | ||
73 | static int parse_format_string(const char *format) | |
74 | { | |
21cbe810 | 75 | for (unsigned int i = 0; i < ARRAY_SIZE(sample_formats); i++) { |
e6b15cb5 SB |
76 | if (!strcmp(format, sample_formats[i].fmt_name)) |
77 | return i; | |
78 | } | |
79 | ||
80 | return -1; | |
81 | } | |
82 | ||
83 | static void init_context(struct context *inc, const struct sample_format *fmt, GSList *channels) | |
84 | { | |
85 | inc->packet.type = SR_DF_ANALOG; | |
86 | inc->packet.payload = &inc->analog; | |
87 | ||
88 | inc->analog.data = NULL; | |
89 | inc->analog.num_samples = 0; | |
90 | inc->analog.encoding = &inc->encoding; | |
91 | inc->analog.meaning = &inc->meaning; | |
92 | inc->analog.spec = &inc->spec; | |
93 | ||
221cec31 | 94 | memcpy(&inc->encoding, &fmt->encoding, sizeof(inc->encoding)); |
e6b15cb5 SB |
95 | |
96 | inc->meaning.mq = 0; | |
97 | inc->meaning.unit = 0; | |
98 | inc->meaning.mqflags = 0; | |
99 | inc->meaning.channels = channels; | |
100 | ||
101 | inc->spec.spec_digits = 0; | |
102 | } | |
103 | ||
104 | static int init(struct sr_input *in, GHashTable *options) | |
105 | { | |
106 | struct context *inc; | |
107 | int num_channels; | |
41b8fca1 | 108 | char channelname[16]; |
e6b15cb5 SB |
109 | const char *format; |
110 | int fmt_index; | |
111 | ||
112 | num_channels = g_variant_get_int32(g_hash_table_lookup(options, "numchannels")); | |
113 | if (num_channels < 1) { | |
114 | sr_err("Invalid value for numchannels: must be at least 1."); | |
115 | return SR_ERR_ARG; | |
116 | } | |
117 | ||
118 | format = g_variant_get_string(g_hash_table_lookup(options, "format"), NULL); | |
119 | if ((fmt_index = parse_format_string(format)) == -1) { | |
120 | GString *formats = g_string_sized_new(200); | |
21cbe810 | 121 | for (unsigned int i = 0; i < ARRAY_SIZE(sample_formats); i++) |
e6b15cb5 SB |
122 | g_string_append_printf(formats, "%s ", sample_formats[i].fmt_name); |
123 | sr_err("Invalid format '%s': must be one of: %s.", | |
124 | format, formats->str); | |
125 | g_string_free(formats, TRUE); | |
126 | return SR_ERR_ARG; | |
127 | } | |
128 | ||
129 | in->sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
130 | in->priv = inc = g_malloc0(sizeof(struct context)); | |
131 | ||
132 | for (int i = 0; i < num_channels; i++) { | |
41b8fca1 | 133 | snprintf(channelname, sizeof(channelname) - 1, "CH%d", i + 1); |
e6b15cb5 SB |
134 | sr_channel_new(in->sdi, i, SR_CHANNEL_ANALOG, TRUE, channelname); |
135 | } | |
136 | ||
137 | inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate")); | |
221cec31 | 138 | inc->samplesize = sample_formats[fmt_index].encoding.unitsize * num_channels; |
e6b15cb5 SB |
139 | init_context(inc, &sample_formats[fmt_index], in->sdi->channels); |
140 | ||
141 | return SR_OK; | |
142 | } | |
143 | ||
144 | static int process_buffer(struct sr_input *in) | |
145 | { | |
146 | struct context *inc; | |
147 | struct sr_datafeed_meta meta; | |
148 | struct sr_datafeed_packet packet; | |
149 | struct sr_config *src; | |
150 | unsigned int offset, chunk_size; | |
151 | ||
152 | inc = in->priv; | |
153 | if (!inc->started) { | |
bee2b016 | 154 | std_session_send_df_header(in->sdi); |
e6b15cb5 SB |
155 | |
156 | if (inc->samplerate) { | |
157 | packet.type = SR_DF_META; | |
158 | packet.payload = &meta; | |
159 | src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate)); | |
160 | meta.config = g_slist_append(NULL, src); | |
161 | sr_session_send(in->sdi, &packet); | |
162 | g_slist_free(meta.config); | |
163 | sr_config_free(src); | |
164 | } | |
165 | ||
166 | inc->started = TRUE; | |
167 | } | |
168 | ||
169 | /* Round down to the last channels * unitsize boundary. */ | |
170 | inc->analog.num_samples = CHUNK_SIZE / inc->samplesize; | |
171 | chunk_size = inc->analog.num_samples * inc->samplesize; | |
172 | offset = 0; | |
173 | ||
174 | while ((offset + chunk_size) < in->buf->len) { | |
175 | inc->analog.data = in->buf->str + offset; | |
176 | sr_session_send(in->sdi, &inc->packet); | |
177 | offset += chunk_size; | |
178 | } | |
179 | ||
180 | inc->analog.num_samples = (in->buf->len - offset) / inc->samplesize; | |
181 | chunk_size = inc->analog.num_samples * inc->samplesize; | |
182 | if (chunk_size > 0) { | |
183 | inc->analog.data = in->buf->str + offset; | |
184 | sr_session_send(in->sdi, &inc->packet); | |
185 | offset += chunk_size; | |
186 | } | |
187 | ||
188 | if ((unsigned int)offset < in->buf->len) { | |
189 | /* | |
190 | * The incoming buffer wasn't processed completely. Stash | |
191 | * the leftover data for next time. | |
192 | */ | |
193 | g_string_erase(in->buf, 0, offset); | |
194 | } else { | |
195 | g_string_truncate(in->buf, 0); | |
196 | } | |
197 | ||
198 | return SR_OK; | |
199 | } | |
200 | ||
201 | static int receive(struct sr_input *in, GString *buf) | |
202 | { | |
203 | int ret; | |
204 | ||
205 | g_string_append_len(in->buf, buf->str, buf->len); | |
206 | ||
207 | if (!in->sdi_ready) { | |
208 | /* sdi is ready, notify frontend. */ | |
209 | in->sdi_ready = TRUE; | |
210 | return SR_OK; | |
211 | } | |
212 | ||
213 | ret = process_buffer(in); | |
214 | ||
215 | return ret; | |
216 | } | |
217 | ||
218 | static int end(struct sr_input *in) | |
219 | { | |
e6b15cb5 SB |
220 | struct context *inc; |
221 | int ret; | |
222 | ||
223 | if (in->sdi_ready) | |
224 | ret = process_buffer(in); | |
225 | else | |
226 | ret = SR_OK; | |
227 | ||
228 | inc = in->priv; | |
3be42bc2 | 229 | if (inc->started) |
bee2b016 | 230 | std_session_send_df_end(in->sdi); |
e6b15cb5 SB |
231 | |
232 | return ret; | |
233 | } | |
234 | ||
235 | static struct sr_option options[] = { | |
867293a1 UH |
236 | { "numchannels", "Number of analog channels", "The number of (analog) channels in the data", NULL, NULL }, |
237 | { "samplerate", "Sample rate (Hz)", "The sample rate of the (analog) data in Hz", NULL, NULL }, | |
238 | { "format", "Data format", "The format of the data (data type, signedness, endianness)", NULL, NULL }, | |
e6b15cb5 SB |
239 | ALL_ZERO |
240 | }; | |
241 | ||
242 | static const struct sr_option *get_options(void) | |
243 | { | |
e6b15cb5 SB |
244 | if (!options[0].def) { |
245 | options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS)); | |
246 | options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE)); | |
247 | options[2].def = g_variant_ref_sink(g_variant_new_string(sample_formats[0].fmt_name)); | |
21cbe810 | 248 | for (unsigned int i = 0; i < ARRAY_SIZE(sample_formats); i++) { |
e6b15cb5 SB |
249 | options[2].values = g_slist_append(options[2].values, |
250 | g_variant_ref_sink(g_variant_new_string(sample_formats[i].fmt_name))); | |
251 | } | |
252 | } | |
253 | ||
254 | return options; | |
255 | } | |
256 | ||
257 | static void cleanup(struct sr_input *in) | |
258 | { | |
9e850040 SA |
259 | g_free(in->priv); |
260 | in->priv = NULL; | |
e6b15cb5 | 261 | |
e6b15cb5 SB |
262 | g_variant_unref(options[0].def); |
263 | g_variant_unref(options[1].def); | |
264 | g_variant_unref(options[2].def); | |
265 | g_slist_free_full(options[2].values, (GDestroyNotify)g_variant_unref); | |
e6b15cb5 SB |
266 | } |
267 | ||
4c55ea47 SA |
268 | static int reset(struct sr_input *in) |
269 | { | |
270 | struct context *inc = in->priv; | |
271 | ||
4c55ea47 | 272 | inc->started = FALSE; |
9e850040 | 273 | |
4c55ea47 SA |
274 | g_string_truncate(in->buf, 0); |
275 | ||
276 | return SR_OK; | |
277 | } | |
278 | ||
e6b15cb5 SB |
279 | SR_PRIV struct sr_input_module input_raw_analog = { |
280 | .id = "raw_analog", | |
281 | .name = "RAW analog", | |
b20eb520 | 282 | .desc = "Raw analog data without header", |
e6b15cb5 SB |
283 | .exts = (const char*[]){"raw", "bin", NULL}, |
284 | .options = get_options, | |
285 | .init = init, | |
286 | .receive = receive, | |
287 | .end = end, | |
288 | .cleanup = cleanup, | |
4c55ea47 | 289 | .reset = reset, |
e6b15cb5 | 290 | }; |