]> sigrok.org Git - libsigrok.git/blame - src/input/raw_analog.c
input/raw_analog: set scale and offset appropriately
[libsigrok.git] / src / input / raw_analog.c
CommitLineData
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. */
34#define CHUNK_SIZE 4096
35#define DEFAULT_NUM_CHANNELS 1
36#define DEFAULT_SAMPLERATE 0
37
38struct 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
50struct sample_format {
51 const char const* fmt_name;
221cec31 52 struct sr_analog_encoding encoding;
e6b15cb5
SB
53};
54
55static const struct sample_format const sample_formats[] =
56{
221cec31
SB
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, 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, 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
73static int parse_format_string(const char *format)
74{
75 int num_formats = sizeof(sample_formats)/ sizeof(sample_formats[0]);
76 for (int i = 0; i < num_formats; i++) {
77 if (!strcmp(format, sample_formats[i].fmt_name))
78 return i;
79 }
80
81 return -1;
82}
83
84static void init_context(struct context *inc, const struct sample_format *fmt, GSList *channels)
85{
86 inc->packet.type = SR_DF_ANALOG;
87 inc->packet.payload = &inc->analog;
88
89 inc->analog.data = NULL;
90 inc->analog.num_samples = 0;
91 inc->analog.encoding = &inc->encoding;
92 inc->analog.meaning = &inc->meaning;
93 inc->analog.spec = &inc->spec;
94
221cec31 95 memcpy(&inc->encoding, &fmt->encoding, sizeof(inc->encoding));
e6b15cb5
SB
96
97 inc->meaning.mq = 0;
98 inc->meaning.unit = 0;
99 inc->meaning.mqflags = 0;
100 inc->meaning.channels = channels;
101
102 inc->spec.spec_digits = 0;
103}
104
105static int init(struct sr_input *in, GHashTable *options)
106{
107 struct context *inc;
108 int num_channels;
109 char channelname[8];
110 const char *format;
111 int fmt_index;
112
113 num_channels = g_variant_get_int32(g_hash_table_lookup(options, "numchannels"));
114 if (num_channels < 1) {
115 sr_err("Invalid value for numchannels: must be at least 1.");
116 return SR_ERR_ARG;
117 }
118
119 format = g_variant_get_string(g_hash_table_lookup(options, "format"), NULL);
120 if ((fmt_index = parse_format_string(format)) == -1) {
121 GString *formats = g_string_sized_new(200);
122 int num_formats = sizeof(sample_formats)/ sizeof(sample_formats[0]);
123 for (int i = 0; i < num_formats; i++)
124 g_string_append_printf(formats, "%s ", sample_formats[i].fmt_name);
125 sr_err("Invalid format '%s': must be one of: %s.",
126 format, formats->str);
127 g_string_free(formats, TRUE);
128 return SR_ERR_ARG;
129 }
130
131 in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
132 in->priv = inc = g_malloc0(sizeof(struct context));
133
134 for (int i = 0; i < num_channels; i++) {
135 snprintf(channelname, 8, "CH%d", i + 1);
136 sr_channel_new(in->sdi, i, SR_CHANNEL_ANALOG, TRUE, channelname);
137 }
138
139 inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
221cec31 140 inc->samplesize = sample_formats[fmt_index].encoding.unitsize * num_channels;
e6b15cb5
SB
141 init_context(inc, &sample_formats[fmt_index], in->sdi->channels);
142
143 return SR_OK;
144}
145
146static int process_buffer(struct sr_input *in)
147{
148 struct context *inc;
149 struct sr_datafeed_meta meta;
150 struct sr_datafeed_packet packet;
151 struct sr_config *src;
152 unsigned int offset, chunk_size;
153
154 inc = in->priv;
155 if (!inc->started) {
156 std_session_send_df_header(in->sdi, LOG_PREFIX);
157
158 if (inc->samplerate) {
159 packet.type = SR_DF_META;
160 packet.payload = &meta;
161 src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate));
162 meta.config = g_slist_append(NULL, src);
163 sr_session_send(in->sdi, &packet);
164 g_slist_free(meta.config);
165 sr_config_free(src);
166 }
167
168 inc->started = TRUE;
169 }
170
171 /* Round down to the last channels * unitsize boundary. */
172 inc->analog.num_samples = CHUNK_SIZE / inc->samplesize;
173 chunk_size = inc->analog.num_samples * inc->samplesize;
174 offset = 0;
175
176 while ((offset + chunk_size) < in->buf->len) {
177 inc->analog.data = in->buf->str + offset;
178 sr_session_send(in->sdi, &inc->packet);
179 offset += chunk_size;
180 }
181
182 inc->analog.num_samples = (in->buf->len - offset) / inc->samplesize;
183 chunk_size = inc->analog.num_samples * inc->samplesize;
184 if (chunk_size > 0) {
185 inc->analog.data = in->buf->str + offset;
186 sr_session_send(in->sdi, &inc->packet);
187 offset += chunk_size;
188 }
189
190 if ((unsigned int)offset < in->buf->len) {
191 /*
192 * The incoming buffer wasn't processed completely. Stash
193 * the leftover data for next time.
194 */
195 g_string_erase(in->buf, 0, offset);
196 } else {
197 g_string_truncate(in->buf, 0);
198 }
199
200 return SR_OK;
201}
202
203static int receive(struct sr_input *in, GString *buf)
204{
205 int ret;
206
207 g_string_append_len(in->buf, buf->str, buf->len);
208
209 if (!in->sdi_ready) {
210 /* sdi is ready, notify frontend. */
211 in->sdi_ready = TRUE;
212 return SR_OK;
213 }
214
215 ret = process_buffer(in);
216
217 return ret;
218}
219
220static int end(struct sr_input *in)
221{
222 struct sr_datafeed_packet packet;
223 struct context *inc;
224 int ret;
225
226 if (in->sdi_ready)
227 ret = process_buffer(in);
228 else
229 ret = SR_OK;
230
231 inc = in->priv;
232 if (inc->started) {
233 packet.type = SR_DF_END;
234 sr_session_send(in->sdi, &packet);
235 }
236
237 return ret;
238}
239
240static struct sr_option options[] = {
241 { "numchannels", "Number of channels", "Number of channels", NULL, NULL },
242 { "samplerate", "Sample rate", "Sample rate", NULL, NULL },
243 { "format", "Format", "Numeric format", NULL, NULL },
244 ALL_ZERO
245};
246
247static const struct sr_option *get_options(void)
248{
249 int num_formats = sizeof(sample_formats)/ sizeof(sample_formats[0]);
250 if (!options[0].def) {
251 options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS));
252 options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE));
253 options[2].def = g_variant_ref_sink(g_variant_new_string(sample_formats[0].fmt_name));
254 for (int i = 0; i < num_formats; i++) {
255 options[2].values = g_slist_append(options[2].values,
256 g_variant_ref_sink(g_variant_new_string(sample_formats[i].fmt_name)));
257 }
258 }
259
260 return options;
261}
262
263static void cleanup(struct sr_input *in)
264{
265 struct context *inc;
266
267 inc = in->priv;
268 g_variant_unref(options[0].def);
269 g_variant_unref(options[1].def);
270 g_variant_unref(options[2].def);
271 g_slist_free_full(options[2].values, (GDestroyNotify)g_variant_unref);
272 g_free(inc);
273 in->priv = NULL;
274}
275
276SR_PRIV struct sr_input_module input_raw_analog = {
277 .id = "raw_analog",
278 .name = "RAW analog",
279 .desc = "analog signals without header",
280 .exts = (const char*[]){"raw", "bin", NULL},
281 .options = get_options,
282 .init = init,
283 .receive = receive,
284 .end = end,
285 .cleanup = cleanup,
286};