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