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