]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
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 | |
17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | /* | |
21 | * Options and their values: | |
22 | * | |
23 | * gnuplot: Write out a gnuplot interpreter script (.gpi file) to plot | |
24 | * the datafile using the parameters given. It should be called | |
25 | * from a gnuplot session with the data file name as a parameter | |
26 | * after adjusting line styles, terminal, etc. | |
27 | * | |
28 | * scale: The gnuplot graphs are scaled so they all have the same | |
29 | * peak-to-peak distance. Defaults to TRUE. | |
30 | * | |
31 | * value: The string used to separate values in a record. Defaults to ','. | |
32 | * | |
33 | * record: The string to use to separate records. Default is newline. gnuplot | |
34 | * files must use newline. | |
35 | * | |
36 | * frame: The string to use when a frame ends. The default is a blank line. | |
37 | * This may confuse some CSV parsers, but it makes gnuplot happy. | |
38 | * | |
39 | * comment: The string that starts a comment line. Defaults to ';'. | |
40 | * | |
41 | * header: Print header comment with capture metadata. Defaults to TRUE. | |
42 | * | |
43 | * label: What to use for channel labels as the first line of output. | |
44 | * Values are "channel", "units", "off". Defaults to "units". | |
45 | * | |
46 | * time: Whether or not the first column should include the time the sample | |
47 | * was taken. Defaults to FALSE. | |
48 | * | |
49 | * trigger: Whether or not to add a "trigger" column as the last column. | |
50 | * Defaults to FALSE. | |
51 | * | |
52 | * dedup: Don't output duplicate rows. Defaults to FALSE. If time is off, then | |
53 | * this is forced to be off. | |
54 | */ | |
55 | ||
56 | #include <config.h> | |
57 | #include <math.h> | |
58 | #include <stdlib.h> | |
59 | #include <string.h> | |
60 | #include <glib.h> | |
61 | #include <libsigrok/libsigrok.h> | |
62 | #include "libsigrok-internal.h" | |
63 | ||
64 | #define LOG_PREFIX "output/csv" | |
65 | ||
66 | struct ctx_channel { | |
67 | struct sr_channel *ch; | |
68 | char *label; | |
69 | float min, max; | |
70 | }; | |
71 | ||
72 | struct context { | |
73 | /* Options */ | |
74 | const char *gnuplot; | |
75 | gboolean scale; | |
76 | const char *value; | |
77 | const char *record; | |
78 | const char *frame; | |
79 | const char *comment; | |
80 | gboolean header, did_header; | |
81 | gboolean label_do, label_did, label_names; | |
82 | gboolean time; | |
83 | gboolean do_trigger; | |
84 | gboolean dedup; | |
85 | ||
86 | /* Plot data */ | |
87 | unsigned int num_analog_channels; | |
88 | unsigned int num_logic_channels; | |
89 | struct ctx_channel *channels; | |
90 | ||
91 | /* Metadata */ | |
92 | gboolean trigger; | |
93 | uint32_t num_samples; | |
94 | uint32_t channel_count, logic_channel_count; | |
95 | uint32_t channels_seen; | |
96 | uint64_t sample_rate; | |
97 | uint64_t sample_scale; | |
98 | uint64_t out_sample_count; | |
99 | uint8_t *previous_sample; | |
100 | float *analog_samples; | |
101 | uint8_t *logic_samples; | |
102 | const char *xlabel; /* Don't free: will point to a static string. */ | |
103 | const char *title; /* Don't free: will point into the driver struct. */ | |
104 | ||
105 | /* Input data constraints check. */ | |
106 | gboolean have_checked; | |
107 | gboolean have_frames; | |
108 | uint64_t pkt_snums; | |
109 | }; | |
110 | ||
111 | /* | |
112 | * TODO: | |
113 | * - Option to print comma-separated bits, or whole bytes/words (for 8/16 | |
114 | * channel LAs) as ASCII/hex etc. etc. | |
115 | */ | |
116 | ||
117 | static int init(struct sr_output *o, GHashTable *options) | |
118 | { | |
119 | unsigned int i, analog_channels, logic_channels; | |
120 | struct context *ctx; | |
121 | struct sr_channel *ch; | |
122 | const char *label_string; | |
123 | GSList *l; | |
124 | ||
125 | if (!o || !o->sdi) | |
126 | return SR_ERR_ARG; | |
127 | ||
128 | ctx = g_malloc0(sizeof(struct context)); | |
129 | o->priv = ctx; | |
130 | ||
131 | /* Options */ | |
132 | ctx->gnuplot = g_strdup(g_variant_get_string( | |
133 | g_hash_table_lookup(options, "gnuplot"), NULL)); | |
134 | ctx->scale = g_variant_get_boolean(g_hash_table_lookup(options, "scale")); | |
135 | ctx->value = g_strdup(g_variant_get_string( | |
136 | g_hash_table_lookup(options, "value"), NULL)); | |
137 | ctx->record = g_strdup(g_variant_get_string( | |
138 | g_hash_table_lookup(options, "record"), NULL)); | |
139 | ctx->frame = g_strdup(g_variant_get_string( | |
140 | g_hash_table_lookup(options, "frame"), NULL)); | |
141 | ctx->comment = g_strdup(g_variant_get_string( | |
142 | g_hash_table_lookup(options, "comment"), NULL)); | |
143 | ctx->header = g_variant_get_boolean(g_hash_table_lookup(options, "header")); | |
144 | ctx->time = g_variant_get_boolean(g_hash_table_lookup(options, "time")); | |
145 | ctx->do_trigger = g_variant_get_boolean(g_hash_table_lookup(options, "trigger")); | |
146 | label_string = g_variant_get_string( | |
147 | g_hash_table_lookup(options, "label"), NULL); | |
148 | ctx->dedup = g_variant_get_boolean(g_hash_table_lookup(options, "dedup")); | |
149 | ctx->dedup &= ctx->time; | |
150 | ||
151 | if (*ctx->gnuplot && g_strcmp0(ctx->record, "\n")) | |
152 | sr_warn("gnuplot record separator must be newline."); | |
153 | ||
154 | if (*ctx->gnuplot && strlen(ctx->value) > 1) | |
155 | sr_warn("gnuplot doesn't support multichar value separators."); | |
156 | ||
157 | if ((ctx->label_did = ctx->label_do = g_strcmp0(label_string, "off") != 0)) | |
158 | ctx->label_names = g_strcmp0(label_string, "units") != 0; | |
159 | ||
160 | sr_dbg("gnuplot = '%s', scale = %d", ctx->gnuplot, ctx->scale); | |
161 | sr_dbg("value = '%s', record = '%s', frame = '%s', comment = '%s'", | |
162 | ctx->value, ctx->record, ctx->frame, ctx->comment); | |
163 | sr_dbg("header = %d, time = %d, do_trigger = %d, dedup = %d", | |
164 | ctx->header, ctx->time, ctx->do_trigger, ctx->dedup); | |
165 | sr_dbg("label_do = %d, label_names = %d", ctx->label_do, ctx->label_names); | |
166 | ||
167 | analog_channels = logic_channels = 0; | |
168 | /* Get the number of channels, and the unitsize. */ | |
169 | for (l = o->sdi->channels; l; l = l->next) { | |
170 | ch = l->data; | |
171 | if (ch->type == SR_CHANNEL_LOGIC) { | |
172 | ctx->logic_channel_count++; | |
173 | if (ch->enabled) | |
174 | logic_channels++; | |
175 | } | |
176 | if (ch->type == SR_CHANNEL_ANALOG && ch->enabled) | |
177 | analog_channels++; | |
178 | } | |
179 | if (analog_channels) { | |
180 | sr_info("Outputting %d analog values", analog_channels); | |
181 | ctx->num_analog_channels = analog_channels; | |
182 | } | |
183 | if (logic_channels) { | |
184 | sr_info("Outputting %d logic values", logic_channels); | |
185 | ctx->num_logic_channels = logic_channels; | |
186 | } | |
187 | ctx->channels = g_malloc(sizeof(struct ctx_channel) | |
188 | * (ctx->num_analog_channels + ctx->num_logic_channels)); | |
189 | ||
190 | /* Once more to map the enabled channels. */ | |
191 | ctx->channel_count = g_slist_length(o->sdi->channels); | |
192 | for (i = 0, l = o->sdi->channels; l; l = l->next) { | |
193 | ch = l->data; | |
194 | if (ch->enabled) { | |
195 | if (ch->type == SR_CHANNEL_ANALOG) { | |
196 | ctx->channels[i].min = FLT_MAX; | |
197 | ctx->channels[i].max = FLT_MIN; | |
198 | } else if (ch->type == SR_CHANNEL_LOGIC) { | |
199 | ctx->channels[i].min = 0; | |
200 | ctx->channels[i].max = 1; | |
201 | } else { | |
202 | sr_warn("Unknown channel type %d.", ch->type); | |
203 | } | |
204 | if (ctx->label_do && ctx->label_names) | |
205 | ctx->channels[i].label = ch->name; | |
206 | ctx->channels[i++].ch = ch; | |
207 | } | |
208 | } | |
209 | ||
210 | return SR_OK; | |
211 | } | |
212 | ||
213 | static const char *xlabels[] = { | |
214 | "samples", "milliseconds", "microseconds", "nanoseconds", "picoseconds", | |
215 | "femtoseconds", "attoseconds", | |
216 | }; | |
217 | ||
218 | static GString *gen_header(const struct sr_output *o, | |
219 | const struct sr_datafeed_header *hdr) | |
220 | { | |
221 | struct context *ctx; | |
222 | struct sr_channel *ch; | |
223 | GVariant *gvar; | |
224 | GString *header; | |
225 | GSList *channels, *l; | |
226 | unsigned int num_channels, i; | |
227 | char *samplerate_s; | |
228 | ||
229 | ctx = o->priv; | |
230 | header = g_string_sized_new(512); | |
231 | ||
232 | if (ctx->sample_rate == 0) { | |
233 | if (sr_config_get(o->sdi->driver, o->sdi, NULL, | |
234 | SR_CONF_SAMPLERATE, &gvar) == SR_OK) { | |
235 | ctx->sample_rate = g_variant_get_uint64(gvar); | |
236 | g_variant_unref(gvar); | |
237 | } | |
238 | ||
239 | i = 0; | |
240 | ctx->sample_scale = 1; | |
241 | while (ctx->sample_scale < ctx->sample_rate) { | |
242 | i++; | |
243 | ctx->sample_scale *= 1000; | |
244 | } | |
245 | if (i < ARRAY_SIZE(xlabels)) | |
246 | ctx->xlabel = xlabels[i]; | |
247 | sr_info("Set sample rate, scale to %" PRIu64 ", %" PRIu64 " %s", | |
248 | ctx->sample_rate, ctx->sample_scale, ctx->xlabel); | |
249 | } | |
250 | ctx->title = (o->sdi && o->sdi->driver) ? o->sdi->driver->longname : "unknown"; | |
251 | ||
252 | /* Some metadata */ | |
253 | if (ctx->header && !ctx->did_header) { | |
254 | /* save_gnuplot knows how many lines we print. */ | |
255 | g_string_append_printf(header, | |
256 | "%s CSV generated by %s %s\n%s from %s on %s", | |
257 | ctx->comment, PACKAGE_NAME, | |
258 | sr_package_version_string_get(), ctx->comment, | |
259 | ctx->title, ctime(&hdr->starttime.tv_sec)); | |
260 | ||
261 | /* Columns / channels */ | |
262 | channels = o->sdi ? o->sdi->channels : NULL; | |
263 | num_channels = g_slist_length(channels); | |
264 | g_string_append_printf(header, "%s Channels (%d/%d):", | |
265 | ctx->comment, ctx->num_analog_channels + | |
266 | ctx->num_logic_channels, num_channels); | |
267 | for (l = channels; l; l = l->next) { | |
268 | ch = l->data; | |
269 | if (ch->enabled) | |
270 | g_string_append_printf(header, " %s,", ch->name); | |
271 | } | |
272 | if (channels) { | |
273 | /* Drop last separator. */ | |
274 | g_string_truncate(header, header->len - 1); | |
275 | } | |
276 | g_string_append_printf(header, "\n"); | |
277 | if (ctx->sample_rate != 0) { | |
278 | samplerate_s = sr_samplerate_string(ctx->sample_rate); | |
279 | g_string_append_printf(header, "%s Samplerate: %s\n", | |
280 | ctx->comment, samplerate_s); | |
281 | g_free(samplerate_s); | |
282 | } | |
283 | ctx->did_header = TRUE; | |
284 | } | |
285 | ||
286 | /* Time column requested but samplerate unknown. Emit a warning. */ | |
287 | if (ctx->time && !ctx->sample_rate) | |
288 | sr_warn("Samplerate unknown, cannot provide timestamps."); | |
289 | ||
290 | return header; | |
291 | } | |
292 | ||
293 | /* | |
294 | * Analog devices can have samples of different types. Since each | |
295 | * packet has only one meaning, it is restricted to having at most one | |
296 | * type of data. So they can send multiple packets for a single sample. | |
297 | * To further complicate things, they can send multiple samples in a | |
298 | * single packet. | |
299 | * | |
300 | * So we need to pull any channels of interest out of a packet and save | |
301 | * them until we have complete samples to output. Some devices make this | |
302 | * simple by sending DF_FRAME_BEGIN/DF_FRAME_END packets, the latter of which | |
303 | * signals the end of a set of samples, so we can dump things there. | |
304 | * | |
305 | * At least one driver (the demo driver) sends packets that contain parts of | |
306 | * multiple samples without wrapping them in DF_FRAME. Possibly this driver | |
307 | * is buggy, but it's also the standard for testing, so it has to be supported | |
308 | * as is. | |
309 | * | |
310 | * Many assumptions about the "shape" of the data here: | |
311 | * | |
312 | * All of the data for a channel is assumed to be in one frame; | |
313 | * otherwise the data in the second packet will overwrite the data in | |
314 | * the first packet. | |
315 | */ | |
316 | static void process_analog(struct context *ctx, | |
317 | const struct sr_datafeed_analog *analog) | |
318 | { | |
319 | int ret; | |
320 | size_t num_rcvd_ch, num_have_ch; | |
321 | size_t idx_have, idx_smpl, idx_rcvd; | |
322 | size_t idx_send; | |
323 | struct sr_analog_meaning *meaning; | |
324 | GSList *l; | |
325 | float *fdata = NULL; | |
326 | struct sr_channel *ch; | |
327 | ||
328 | if (!ctx->analog_samples) { | |
329 | ctx->analog_samples = g_malloc(analog->num_samples | |
330 | * sizeof(float) * ctx->num_analog_channels); | |
331 | if (!ctx->num_samples) | |
332 | ctx->num_samples = analog->num_samples; | |
333 | } | |
334 | if (ctx->num_samples != analog->num_samples) | |
335 | sr_warn("Expecting %u analog samples, got %u.", | |
336 | ctx->num_samples, analog->num_samples); | |
337 | ||
338 | meaning = analog->meaning; | |
339 | num_rcvd_ch = g_slist_length(meaning->channels); | |
340 | ctx->channels_seen += num_rcvd_ch; | |
341 | sr_dbg("Processing packet of %zu analog channels", num_rcvd_ch); | |
342 | fdata = g_malloc(analog->num_samples * num_rcvd_ch * sizeof(float)); | |
343 | if ((ret = sr_analog_to_float(analog, fdata)) != SR_OK) | |
344 | sr_warn("Problems converting data to floating point values."); | |
345 | ||
346 | num_have_ch = ctx->num_analog_channels + ctx->num_logic_channels; | |
347 | idx_send = 0; | |
348 | for (idx_have = 0; idx_have < num_have_ch; idx_have++) { | |
349 | if (ctx->channels[idx_have].ch->type != SR_CHANNEL_ANALOG) | |
350 | continue; | |
351 | sr_dbg("Looking for channel %s", | |
352 | ctx->channels[idx_have].ch->name); | |
353 | for (l = meaning->channels, idx_rcvd = 0; l; l = l->next, idx_rcvd++) { | |
354 | ch = l->data; | |
355 | sr_dbg("Checking %s", ch->name); | |
356 | if (ctx->channels[idx_have].ch != ch) | |
357 | continue; | |
358 | if (ctx->label_do && !ctx->label_names) { | |
359 | sr_analog_unit_to_string(analog, | |
360 | &ctx->channels[idx_have].label); | |
361 | } | |
362 | for (idx_smpl = 0; idx_smpl < analog->num_samples; idx_smpl++) | |
363 | ctx->analog_samples[idx_smpl * ctx->num_analog_channels + idx_send] = fdata[idx_smpl * num_rcvd_ch + idx_rcvd]; | |
364 | break; | |
365 | } | |
366 | idx_send++; | |
367 | } | |
368 | g_free(fdata); | |
369 | } | |
370 | ||
371 | /* | |
372 | * We treat logic packets the same as analog packets, though it's not | |
373 | * strictly required. This allows us to process mixed signals properly. | |
374 | */ | |
375 | static void process_logic(struct context *ctx, | |
376 | const struct sr_datafeed_logic *logic) | |
377 | { | |
378 | unsigned int i, j, ch, num_samples; | |
379 | int idx; | |
380 | uint8_t *sample; | |
381 | ||
382 | num_samples = logic->length / logic->unitsize; | |
383 | ctx->channels_seen += ctx->logic_channel_count; | |
384 | sr_dbg("Logic packet had %d channels", logic->unitsize * 8); | |
385 | if (!ctx->logic_samples) { | |
386 | ctx->logic_samples = g_malloc(num_samples * ctx->num_logic_channels); | |
387 | if (!ctx->num_samples) | |
388 | ctx->num_samples = num_samples; | |
389 | } | |
390 | if (ctx->num_samples != num_samples) | |
391 | sr_warn("Expecting %u samples, got %u", | |
392 | ctx->num_samples, num_samples); | |
393 | ||
394 | for (j = ch = 0; ch < ctx->num_logic_channels; j++) { | |
395 | if (ctx->channels[j].ch->type == SR_CHANNEL_LOGIC) { | |
396 | for (i = 0; i < num_samples; i++) { | |
397 | sample = logic->data + i * logic->unitsize; | |
398 | idx = ctx->channels[j].ch->index; | |
399 | if (ctx->label_do && !ctx->label_names) | |
400 | ctx->channels[j].label = "logic"; | |
401 | ctx->logic_samples[i * ctx->num_logic_channels + ch] = sample[idx / 8] & (1 << (idx % 8)); | |
402 | } | |
403 | ch++; | |
404 | } | |
405 | } | |
406 | } | |
407 | ||
408 | static void dump_saved_values(struct context *ctx, GString **out) | |
409 | { | |
410 | unsigned int i, j, analog_size, num_channels; | |
411 | double sample_time_dbl; | |
412 | uint64_t sample_time_u64; | |
413 | float *analog_sample, value; | |
414 | uint8_t *logic_sample; | |
415 | ||
416 | /* If we haven't seen samples we're expecting, skip them. */ | |
417 | if ((ctx->num_analog_channels && !ctx->analog_samples) || | |
418 | (ctx->num_logic_channels && !ctx->logic_samples)) { | |
419 | sr_warn("Discarding partial packet"); | |
420 | } else { | |
421 | sr_info("Dumping %u samples", ctx->num_samples); | |
422 | ||
423 | if (!*out) | |
424 | *out = g_string_sized_new(512); | |
425 | num_channels = | |
426 | ctx->num_logic_channels + ctx->num_analog_channels; | |
427 | ||
428 | if (ctx->label_do) { | |
429 | if (ctx->time) | |
430 | g_string_append_printf(*out, "%s%s", | |
431 | ctx->label_names ? "Time" : ctx->xlabel, | |
432 | ctx->value); | |
433 | for (i = 0; i < num_channels; i++) { | |
434 | g_string_append_printf(*out, "%s%s", | |
435 | ctx->channels[i].label, ctx->value); | |
436 | if (ctx->channels[i].ch->type == SR_CHANNEL_ANALOG | |
437 | && ctx->label_names) | |
438 | g_free(ctx->channels[i].label); | |
439 | } | |
440 | if (ctx->do_trigger) | |
441 | g_string_append_printf(*out, "Trigger%s", | |
442 | ctx->value); | |
443 | /* Drop last separator. */ | |
444 | g_string_truncate(*out, (*out)->len - 1); | |
445 | g_string_append(*out, ctx->record); | |
446 | ||
447 | ctx->label_do = FALSE; | |
448 | } | |
449 | ||
450 | analog_size = ctx->num_analog_channels * sizeof(float); | |
451 | if (ctx->dedup && !ctx->previous_sample) | |
452 | ctx->previous_sample = g_malloc0(analog_size + ctx->num_logic_channels); | |
453 | ||
454 | for (i = 0; i < ctx->num_samples; i++) { | |
455 | analog_sample = | |
456 | &ctx->analog_samples[i * ctx->num_analog_channels]; | |
457 | logic_sample = | |
458 | &ctx->logic_samples[i * ctx->num_logic_channels]; | |
459 | ||
460 | if (ctx->dedup) { | |
461 | if (i > 0 && i < ctx->num_samples - 1 && | |
462 | !memcmp(logic_sample, ctx->previous_sample, | |
463 | ctx->num_logic_channels) && | |
464 | !memcmp(analog_sample, | |
465 | ctx->previous_sample + | |
466 | ctx->num_logic_channels, | |
467 | analog_size)) | |
468 | continue; | |
469 | memcpy(ctx->previous_sample, logic_sample, | |
470 | ctx->num_logic_channels); | |
471 | memcpy(ctx->previous_sample | |
472 | + ctx->num_logic_channels, | |
473 | analog_sample, analog_size); | |
474 | } | |
475 | ||
476 | if (ctx->time && !ctx->sample_rate) { | |
477 | g_string_append_printf(*out, "0%s", ctx->value); | |
478 | } else if (ctx->time) { | |
479 | sample_time_dbl = ctx->out_sample_count++; | |
480 | sample_time_dbl /= ctx->sample_rate; | |
481 | sample_time_dbl *= ctx->sample_scale; | |
482 | sample_time_u64 = sample_time_dbl; | |
483 | g_string_append_printf(*out, "%" PRIu64 "%s", | |
484 | sample_time_u64, ctx->value); | |
485 | } | |
486 | ||
487 | for (j = 0; j < num_channels; j++) { | |
488 | if (ctx->channels[j].ch->type == SR_CHANNEL_ANALOG) { | |
489 | value = ctx->analog_samples[i * ctx->num_analog_channels + j]; | |
490 | ctx->channels[j].max = | |
491 | fmax(value, ctx->channels[j].max); | |
492 | ctx->channels[j].min = | |
493 | fmin(value, ctx->channels[j].min); | |
494 | g_string_append_printf(*out, "%g%s", | |
495 | value, ctx->value); | |
496 | } else if (ctx->channels[j].ch->type == SR_CHANNEL_LOGIC) { | |
497 | g_string_append_printf(*out, "%c%s", | |
498 | ctx->logic_samples[i * ctx->num_logic_channels + j] ? '1' : '0', ctx->value); | |
499 | } else { | |
500 | sr_warn("Unexpected channel type: %d", | |
501 | ctx->channels[i].ch->type); | |
502 | } | |
503 | } | |
504 | ||
505 | if (ctx->do_trigger) { | |
506 | g_string_append_printf(*out, "%d%s", | |
507 | ctx->trigger, ctx->value); | |
508 | ctx->trigger = FALSE; | |
509 | } | |
510 | g_string_truncate(*out, (*out)->len - 1); | |
511 | g_string_append(*out, ctx->record); | |
512 | } | |
513 | } | |
514 | ||
515 | /* Discard all of the working space. */ | |
516 | g_free(ctx->previous_sample); | |
517 | g_free(ctx->analog_samples); | |
518 | g_free(ctx->logic_samples); | |
519 | ctx->channels_seen = 0; | |
520 | ctx->num_samples = 0; | |
521 | ctx->previous_sample = NULL; | |
522 | ctx->analog_samples = NULL; | |
523 | ctx->logic_samples = NULL; | |
524 | } | |
525 | ||
526 | static void save_gnuplot(struct context *ctx) | |
527 | { | |
528 | float offset, max, sum; | |
529 | unsigned int i, num_channels; | |
530 | GString *script; | |
531 | ||
532 | script = g_string_sized_new(512); | |
533 | g_string_append_printf(script, "set datafile separator '%s'\n", | |
534 | ctx->value); | |
535 | if (ctx->label_did) | |
536 | g_string_append(script, "set key autotitle columnhead\n"); | |
537 | if (ctx->xlabel && ctx->time) | |
538 | g_string_append_printf(script, "set xlabel '%s'\n", | |
539 | ctx->xlabel); | |
540 | ||
541 | g_string_append(script, "plot "); | |
542 | ||
543 | num_channels = ctx->num_analog_channels + ctx->num_logic_channels; | |
544 | ||
545 | /* Graph position and scaling. */ | |
546 | max = FLT_MIN; | |
547 | sum = 0; | |
548 | for (i = 0; i < num_channels; i++) { | |
549 | ctx->channels[i].max = | |
550 | ctx->channels[i].max - ctx->channels[i].min; | |
551 | max = fmax(max, ctx->channels[i].max); | |
552 | sum += ctx->channels[i].max; | |
553 | } | |
554 | sum = (ctx->scale ? max : sum / num_channels) / 4; | |
555 | offset = sum; | |
556 | for (i = num_channels; i > 0;) { | |
557 | i--; | |
558 | ctx->channels[i].min = offset - ctx->channels[i].min; | |
559 | offset += sum + (ctx->scale ? max : ctx->channels[i].max); | |
560 | } | |
561 | ||
562 | for (i = 0; i < num_channels; i++) { | |
563 | sr_spew("Channel %d, min %g, max %g", i, ctx->channels[i].min, | |
564 | ctx->channels[i].max); | |
565 | g_string_append(script, "ARG1 "); | |
566 | if (ctx->did_header) | |
567 | g_string_append(script, "skip 4 "); | |
568 | g_string_append_printf(script, "using %u:($%u * %g + %g), ", | |
569 | ctx->time, i + 1 + ctx->time, ctx->scale ? | |
570 | max / ctx->channels[i].max : 1, ctx->channels[i].min); | |
571 | offset += 1.1 * (ctx->channels[i].max - ctx->channels[i].min); | |
572 | } | |
573 | g_string_truncate(script, script->len - 2); | |
574 | g_file_set_contents(ctx->gnuplot, script->str, script->len, NULL); | |
575 | g_string_free(script, TRUE); | |
576 | } | |
577 | ||
578 | static void check_input_constraints(struct context *ctx) | |
579 | { | |
580 | size_t snum_count, snum_warn_limit; | |
581 | size_t logic, analog; | |
582 | gboolean has_frames, is_short, is_mixed, is_multi_analog; | |
583 | gboolean do_warn; | |
584 | ||
585 | /* | |
586 | * Check and conditionally warn exactly once during execution | |
587 | * of the output module on a set of input data. | |
588 | */ | |
589 | if (ctx->have_checked) | |
590 | return; | |
591 | ctx->have_checked = TRUE; | |
592 | ||
593 | /* | |
594 | * This implementation of the CSV output module assumes some | |
595 | * constraints which need not be met in reality. Emit warnings | |
596 | * until a better version becomes available. Letting users know | |
597 | * that their request may not get processed correctly is the | |
598 | * only thing we can do for now except for complete refusal to | |
599 | * process the input data. | |
600 | * | |
601 | * What the implementation appears to assume (unverified, this | |
602 | * interpretation may be incorrect and/or incomplete): | |
603 | * - Multi-channel analog data, or mixed signal input, always | |
604 | * is enclosed in frame markers. | |
605 | * - Data which gets received across several packets spans a | |
606 | * consistent sample number range. All samples of one frame | |
607 | * and channel number or data type fit into a single packet. | |
608 | * Arbitrary chunking seems to not be supported. | |
609 | * - A specific order of analog data packets is assumed. | |
610 | * | |
611 | * With these assumptions encoded in the implementation, and | |
612 | * not being met at runtime, incorrect and unexpected results | |
613 | * were seen for these configurations: | |
614 | * - More than one analog channel. | |
615 | * - The combination of logic and analog channel types. | |
616 | * | |
617 | * The condition of frames with large sample counts is a wild | |
618 | * guess, the limit is a totally arbitrary choice. It assumes | |
619 | * typical scope frames with at most a few thousand samples per | |
620 | * frame, and assumes that a channel's data gets sent in large | |
621 | * enough packets. The absence of a warning message does not | |
622 | * necessarily translate to correct output, it's more of a rate | |
623 | * limiting approach to not scare users too much. | |
624 | */ | |
625 | snum_count = ctx->pkt_snums; | |
626 | snum_warn_limit = 1 * 1000 * 1000; | |
627 | logic = ctx->num_logic_channels; | |
628 | analog = ctx->num_analog_channels; | |
629 | has_frames = ctx->have_frames; | |
630 | is_short = snum_count < snum_warn_limit; | |
631 | is_mixed = logic && analog; | |
632 | is_multi_analog = analog > 1; | |
633 | ||
634 | if (has_frames && is_short) { | |
635 | sr_info("Assuming consistent framed input data."); | |
636 | return; | |
637 | } | |
638 | ||
639 | do_warn = FALSE; | |
640 | if (has_frames) { | |
641 | sr_warn("Untested configuration: large frame content."); | |
642 | do_warn = TRUE; | |
643 | } | |
644 | if (is_mixed) { | |
645 | sr_warn("Untested configuration: mixed signal input data."); | |
646 | do_warn = TRUE; | |
647 | } | |
648 | if (is_multi_analog) { | |
649 | sr_warn("Untested configuration: multi-channel analog data."); | |
650 | do_warn = TRUE; | |
651 | } | |
652 | if (!do_warn) | |
653 | return; | |
654 | sr_warn("Resulting CSV output data may be incomplete or incorrect."); | |
655 | } | |
656 | ||
657 | static int receive(const struct sr_output *o, | |
658 | const struct sr_datafeed_packet *packet, GString **out) | |
659 | { | |
660 | struct context *ctx; | |
661 | const struct sr_datafeed_logic *logic; | |
662 | const struct sr_datafeed_analog *analog; | |
663 | ||
664 | *out = NULL; | |
665 | if (!o || !o->sdi) | |
666 | return SR_ERR_ARG; | |
667 | if (!(ctx = o->priv)) | |
668 | return SR_ERR_ARG; | |
669 | ||
670 | sr_dbg("Got packet of type %d", packet->type); | |
671 | switch (packet->type) { | |
672 | case SR_DF_HEADER: | |
673 | ctx->have_checked = FALSE; | |
674 | ctx->have_frames = FALSE; | |
675 | ctx->pkt_snums = FALSE; | |
676 | *out = gen_header(o, packet->payload); | |
677 | break; | |
678 | case SR_DF_TRIGGER: | |
679 | ctx->trigger = TRUE; | |
680 | break; | |
681 | case SR_DF_LOGIC: | |
682 | *out = g_string_sized_new(512); | |
683 | logic = packet->payload; | |
684 | ctx->pkt_snums = logic->length; | |
685 | ctx->pkt_snums /= logic->length; | |
686 | check_input_constraints(ctx); | |
687 | process_logic(ctx, logic); | |
688 | break; | |
689 | case SR_DF_ANALOG: | |
690 | *out = g_string_sized_new(512); | |
691 | analog = packet->payload; | |
692 | ctx->pkt_snums = analog->num_samples; | |
693 | ctx->pkt_snums /= g_slist_length(analog->meaning->channels); | |
694 | check_input_constraints(ctx); | |
695 | process_analog(ctx, analog); | |
696 | break; | |
697 | case SR_DF_FRAME_BEGIN: | |
698 | ctx->have_frames = TRUE; | |
699 | *out = g_string_new(ctx->frame); | |
700 | /* Fallthrough */ | |
701 | case SR_DF_END: | |
702 | /* Got to end of frame/session with part of the data. */ | |
703 | if (ctx->channels_seen) | |
704 | ctx->channels_seen = ctx->channel_count; | |
705 | if (*ctx->gnuplot) | |
706 | save_gnuplot(ctx); | |
707 | break; | |
708 | } | |
709 | ||
710 | /* If we've got them all, dump the values. */ | |
711 | if (ctx->channels_seen >= ctx->channel_count) | |
712 | dump_saved_values(ctx, out); | |
713 | ||
714 | return SR_OK; | |
715 | } | |
716 | ||
717 | static int cleanup(struct sr_output *o) | |
718 | { | |
719 | struct context *ctx; | |
720 | ||
721 | if (!o || !o->sdi) | |
722 | return SR_ERR_ARG; | |
723 | ||
724 | if (o->priv) { | |
725 | ctx = o->priv; | |
726 | g_free((gpointer)ctx->record); | |
727 | g_free((gpointer)ctx->frame); | |
728 | g_free((gpointer)ctx->comment); | |
729 | g_free((gpointer)ctx->gnuplot); | |
730 | g_free((gpointer)ctx->value); | |
731 | g_free(ctx->previous_sample); | |
732 | g_free(ctx->channels); | |
733 | g_free(o->priv); | |
734 | o->priv = NULL; | |
735 | } | |
736 | ||
737 | return SR_OK; | |
738 | } | |
739 | ||
740 | static struct sr_option options[] = { | |
741 | {"gnuplot", "gnuplot", "gnuplot script file name", NULL, NULL}, | |
742 | {"scale", "scale", "Scale gnuplot graphs", NULL, NULL}, | |
743 | {"value", "Value separator", "Character to print between values", NULL, NULL}, | |
744 | {"record", "Record separator", "String to print between records", NULL, NULL}, | |
745 | {"frame", "Frame separator", "String to print between frames", NULL, NULL}, | |
746 | {"comment", "Comment start string", "String used at start of comment lines", NULL, NULL}, | |
747 | {"header", "Output header", "Output header comment with capture metdata", NULL, NULL}, | |
748 | {"label", "Label values", "Type of column labels", NULL, NULL}, | |
749 | {"time", "Time column", "Output sample time as column 1", NULL, NULL}, | |
750 | {"trigger", "Trigger column", "Output trigger indicator as last column ", NULL, NULL}, | |
751 | {"dedup", "Dedup rows", "Set to false to output duplicate rows", NULL, NULL}, | |
752 | ALL_ZERO | |
753 | }; | |
754 | ||
755 | static const struct sr_option *get_options(void) | |
756 | { | |
757 | GSList *l = NULL; | |
758 | ||
759 | if (!options[0].def) { | |
760 | options[0].def = g_variant_ref_sink(g_variant_new_string("")); | |
761 | options[1].def = g_variant_ref_sink(g_variant_new_boolean(TRUE)); | |
762 | options[2].def = g_variant_ref_sink(g_variant_new_string(",")); | |
763 | options[3].def = g_variant_ref_sink(g_variant_new_string("\n")); | |
764 | options[4].def = g_variant_ref_sink(g_variant_new_string("\n")); | |
765 | options[5].def = g_variant_ref_sink(g_variant_new_string(";")); | |
766 | options[6].def = g_variant_ref_sink(g_variant_new_boolean(TRUE)); | |
767 | options[7].def = g_variant_ref_sink(g_variant_new_string("units")); | |
768 | l = g_slist_append(l, g_variant_ref_sink(g_variant_new_string("units"))); | |
769 | l = g_slist_append(l, g_variant_ref_sink(g_variant_new_string("channel"))); | |
770 | l = g_slist_append(l, g_variant_ref_sink(g_variant_new_string("off"))); | |
771 | options[7].values = l; | |
772 | options[8].def = g_variant_ref_sink(g_variant_new_boolean(FALSE)); | |
773 | options[9].def = g_variant_ref_sink(g_variant_new_boolean(FALSE)); | |
774 | options[10].def = g_variant_ref_sink(g_variant_new_boolean(FALSE)); | |
775 | } | |
776 | ||
777 | return options; | |
778 | } | |
779 | ||
780 | SR_PRIV struct sr_output_module output_csv = { | |
781 | .id = "csv", | |
782 | .name = "CSV", | |
783 | .desc = "Comma-separated values", | |
784 | .exts = (const char *[]){"csv", NULL}, | |
785 | .flags = 0, | |
786 | .options = get_options, | |
787 | .init = init, | |
788 | .receive = receive, | |
789 | .cleanup = cleanup, | |
790 | }; |