]> sigrok.org Git - libsigrok.git/blame - src/output/csv.c
output/csv: fix segfault when naming logic channels
[libsigrok.git] / src / output / csv.c
CommitLineData
02604ed6 1/*
50985c20 2 * This file is part of the libsigrok project.
02604ed6
UH
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
2ea1fdf1 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
02604ed6
UH
18 */
19
9e24c8bc
MM
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 *
cad447d2
MM
43 * label: What to use for channel labels as the first line of output.
44 * Values are "channel", "units", "off". Defaults to "units".
9e24c8bc
MM
45 *
46 * time: Whether or not the first column should include the time the sample
47 * was taken. Defaults to TRUE.
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 TRUE. If time is off, then
53 * this is forced to be off.
54 */
55
56#include <math.h>
6ec6c43b 57#include <config.h>
02604ed6
UH
58#include <stdlib.h>
59#include <string.h>
60#include <glib.h>
c1aae900 61#include <libsigrok/libsigrok.h>
45c59c8b 62#include "libsigrok-internal.h"
02604ed6 63
3544f848 64#define LOG_PREFIX "output/csv"
a944a84b 65
9e24c8bc
MM
66struct ctx_channel {
67 struct sr_channel *ch;
cad447d2 68 char *label;
9e24c8bc
MM
69 float min, max;
70};
71
02604ed6 72struct context {
9e24c8bc
MM
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;
cad447d2 81 gboolean label_do, label_did, label_names;
9e24c8bc
MM
82 gboolean time;
83 gboolean do_trigger;
84 gboolean dedup;
85
86 /* Plot data */
c04cf9aa 87 unsigned int num_analog_channels;
9e24c8bc
MM
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 period;
97 uint64_t sample_time;
98 uint8_t *previous_sample;
99 float *analog_samples;
100 uint8_t *logic_samples;
101 const char *xlabel; /* Don't free: will point to a static string. */
102 const char *title; /* Don't free: will point into the driver struct. */
02604ed6
UH
103};
104
105/*
106 * TODO:
02604ed6 107 * - Option to print comma-separated bits, or whole bytes/words (for 8/16
ba7dd8bb 108 * channel LAs) as ASCII/hex etc. etc.
02604ed6
UH
109 */
110
a755b0e1 111static int init(struct sr_output *o, GHashTable *options)
02604ed6 112{
9e24c8bc 113 unsigned int i, analog_channels, logic_channels;
02604ed6 114 struct context *ctx;
ba7dd8bb 115 struct sr_channel *ch;
cad447d2 116 const char *label_string;
02604ed6 117 GSList *l;
a755b0e1 118
0f3d8c89 119 if (!o || !o->sdi)
02604ed6 120 return SR_ERR_ARG;
02604ed6 121
2a035e53 122 ctx = g_malloc0(sizeof(struct context));
d686c5ec 123 o->priv = ctx;
02604ed6 124
9e24c8bc
MM
125 /* Options */
126 ctx->gnuplot = g_strdup(g_variant_get_string(
127 g_hash_table_lookup(options, "gnuplot"), NULL));
128 ctx->scale = g_variant_get_boolean(g_hash_table_lookup(options, "scale"));
129 ctx->value = g_strdup(g_variant_get_string(
130 g_hash_table_lookup(options, "value"), NULL));
131 ctx->record = g_strdup(g_variant_get_string(
132 g_hash_table_lookup(options, "record"), NULL));
133 ctx->frame = g_strdup(g_variant_get_string(
134 g_hash_table_lookup(options, "frame"), NULL));
135 ctx->comment = g_strdup(g_variant_get_string(
136 g_hash_table_lookup(options, "comment"), NULL));
137 ctx->header = g_variant_get_boolean(g_hash_table_lookup(options, "header"));
138 ctx->time = g_variant_get_boolean(g_hash_table_lookup(options, "time"));
139 ctx->do_trigger = g_variant_get_boolean(g_hash_table_lookup(options, "trigger"));
cad447d2
MM
140 label_string = g_variant_get_string(
141 g_hash_table_lookup(options, "label"), NULL);
9e24c8bc
MM
142 ctx->dedup = g_variant_get_boolean(g_hash_table_lookup(options, "dedup"));
143 ctx->dedup &= ctx->time;
144
145 if (*ctx->gnuplot && g_strcmp0(ctx->record, "\n"))
146 sr_warn("gnuplot record separator must be newline.");
147
148 if (*ctx->gnuplot && strlen(ctx->value) > 1)
149 sr_warn("gnuplot doesn't support multichar value separators.");
150
cad447d2
MM
151 if ((ctx->label_did = ctx->label_do = g_strcmp0(label_string, "off") != 0))
152 ctx->label_names = g_strcmp0(label_string, "units") != 0;
153
9e24c8bc
MM
154 sr_dbg("gnuplot = '%s', scale = %d", ctx->gnuplot, ctx->scale);
155 sr_dbg("value = '%s', record = '%s', frame = '%s', comment = '%s'",
156 ctx->value, ctx->record, ctx->frame, ctx->comment);
cad447d2
MM
157 sr_dbg("header = %d, time = %d, do_trigger = %d, dedup = %d",
158 ctx->header, ctx->time, ctx->do_trigger, ctx->dedup);
159 sr_dbg("label_do = %d, label_names = %d", ctx->label_do, ctx->label_names);
9e24c8bc
MM
160
161 analog_channels = logic_channels = 0;
ba7dd8bb
UH
162 /* Get the number of channels, and the unitsize. */
163 for (l = o->sdi->channels; l; l = l->next) {
164 ch = l->data;
9e24c8bc
MM
165 if (ch->type == SR_CHANNEL_LOGIC) {
166 ctx->logic_channel_count++;
167 if (ch->enabled)
168 logic_channels++;
c04cf9aa 169 }
9e24c8bc
MM
170 if (ch->type == SR_CHANNEL_ANALOG && ch->enabled)
171 analog_channels++;
172 }
173 if (analog_channels) {
174 sr_info("Outputting %d analog values", analog_channels);
175 ctx->num_analog_channels = analog_channels;
176 }
177 if (logic_channels) {
178 sr_info("Outputting %d logic values", logic_channels);
179 ctx->num_logic_channels = logic_channels;
02604ed6 180 }
9e24c8bc
MM
181 ctx->channels = g_malloc(sizeof(struct ctx_channel)
182 * (ctx->num_analog_channels + ctx->num_logic_channels));
02604ed6 183
0f3d8c89 184 /* Once more to map the enabled channels. */
9e24c8bc
MM
185 ctx->channel_count = g_slist_length(o->sdi->channels);
186 for (i = 0, l = o->sdi->channels; l; l = l->next) {
0f3d8c89 187 ch = l->data;
c04cf9aa 188 if (ch->enabled) {
9e24c8bc
MM
189 if (ch->type == SR_CHANNEL_ANALOG) {
190 ctx->channels[i].min = FLT_MAX;
191 ctx->channels[i].max = FLT_MIN;
192 } else if (ch->type == SR_CHANNEL_LOGIC) {
193 ctx->channels[i].min = 0;
194 ctx->channels[i].max = 1;
195 } else {
196 sr_warn("Unknown channel type %d.", ch->type);
197 }
cad447d2
MM
198 if (ctx->label_do && ctx->label_names)
199 ctx->channels[i].label = ch->name;
9e24c8bc 200 ctx->channels[i++].ch = ch;
c04cf9aa 201 }
0f3d8c89 202 }
02604ed6 203
0f3d8c89
BV
204 return SR_OK;
205}
02604ed6 206
9e24c8bc
MM
207static const char *xlabels[] = {
208 "samples", "milliseconds", "microseconds", "nanoseconds", "picoseconds",
209 "femtoseconds", "attoseconds",
210};
211
212static GString *gen_header(const struct sr_output *o,
213 const struct sr_datafeed_header *hdr)
0f3d8c89
BV
214{
215 struct context *ctx;
216 struct sr_channel *ch;
217 GVariant *gvar;
218 GString *header;
219 GSList *l;
9e24c8bc
MM
220 unsigned int num_channels, i;
221 uint64_t samplerate = 0, sr;
0f3d8c89 222 char *samplerate_s;
02604ed6 223
d686c5ec 224 ctx = o->priv;
0f3d8c89 225 header = g_string_sized_new(512);
02604ed6 226
9e24c8bc
MM
227 if (ctx->period == 0) {
228 if (sr_config_get(o->sdi->driver, o->sdi, NULL,
229 SR_CONF_SAMPLERATE, &gvar) == SR_OK) {
230 samplerate = g_variant_get_uint64(gvar);
0f3d8c89
BV
231 g_variant_unref(gvar);
232 }
9e24c8bc
MM
233
234 i = 0;
235 sr = 1;
236 while (sr < samplerate) {
237 i++;
238 sr *= 1000;
239 }
240 if (samplerate)
241 ctx->period = sr / samplerate;
242 if (i < ARRAY_SIZE(xlabels))
243 ctx->xlabel = xlabels[i];
244 sr_info("Set sample period to %" PRIu64 " %s",
245 ctx->period, ctx->xlabel);
0f3d8c89 246 }
cf1d5f17 247 ctx->title = (o->sdi && o->sdi->driver) ? o->sdi->driver->longname : "unknown";
9e24c8bc
MM
248
249 /* Some metadata */
250 if (ctx->header && !ctx->did_header) {
251 /* save_gnuplot knows how many lines we print. */
252 g_string_append_printf(header,
253 "%s CSV generated by %s %s\n%s from %s on %s",
254 ctx->comment, PACKAGE_NAME,
255 SR_PACKAGE_VERSION_STRING, ctx->comment,
256 ctx->title, ctime(&hdr->starttime.tv_sec));
257
258 /* Columns / channels */
259 num_channels = g_slist_length(o->sdi->channels);
260 g_string_append_printf(header, "%s Channels (%d/%d):",
261 ctx->comment, ctx->num_analog_channels +
262 ctx->num_logic_channels, num_channels);
263 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
264 ch = l->data;
265 if (ch->enabled)
266 g_string_append_printf(header, " %s,", ch->name);
267 }
268 if (o->sdi->channels)
269 /* Drop last separator. */
270 g_string_truncate(header, header->len - 1);
271 g_string_append_printf(header, "\n");
272 if (samplerate != 0) {
273 samplerate_s = sr_samplerate_string(samplerate);
274 g_string_append_printf(header, "%s Samplerate: %s\n",
275 ctx->comment, samplerate_s);
276 g_free(samplerate_s);
277 }
278 ctx->did_header = TRUE;
0f3d8c89 279 }
02604ed6 280
0f3d8c89 281 return header;
02604ed6
UH
282}
283
9e24c8bc
MM
284/*
285 * Analog devices can have samples of different types. Since each
286 * packet has only one meaning, it is restricted to having at most one
287 * type of data. So they can send multiple packets for a single sample.
288 * To further complicate things, they can send multiple samples in a
289 * single packet.
290 *
291 * So we need to pull any channels of interest out of a packet and save
292 * them until we have complete samples to output. Some devices make this
293 * simple by sending DF_FRAME_BEGIN/DF_FRAME_END packets, the latter of which
294 * signals the end of a set of samples, so we can dump things there.
295 *
296 * At least one driver (the demo driver) sends packets that contain parts of
297 * multiple samples without wrapping them in DF_FRAME. Possibly this driver
298 * is buggy, but it's also the standard for testing, so it has to be supported
299 * as is.
300 *
301 * Many assumptions about the "shape" of the data here:
302 *
303 * All of the data for a channel is assumed to be in one frame;
304 * otherwise the data in the second packet will overwrite the data in
305 * the first packet.
306 */
307static void process_analog(struct context *ctx,
308 const struct sr_datafeed_analog *analog)
309{
310 int ret;
311 unsigned int i, j, c, num_channels;
312 struct sr_analog_meaning *meaning;
313 GSList *l;
314 float *fdata = NULL;
315
316 if (!ctx->analog_samples) {
317 ctx->analog_samples = g_malloc(analog->num_samples
318 * sizeof(float) * ctx->num_analog_channels);
319 if (!ctx->num_samples)
320 ctx->num_samples = analog->num_samples;
321 }
322 if (ctx->num_samples != analog->num_samples)
323 sr_warn("Expecting %u analog samples, got %u.",
324 ctx->num_samples, analog->num_samples);
325
326 meaning = analog->meaning;
327 num_channels = g_slist_length(meaning->channels);
328 ctx->channels_seen += num_channels;
329 sr_dbg("Processing packet of %u analog channels", num_channels);
e34dbc9d 330 fdata = g_malloc(analog->num_samples * num_channels * sizeof(float));
9e24c8bc
MM
331 if ((ret = sr_analog_to_float(analog, fdata)) != SR_OK)
332 sr_warn("Problems converting data to floating point values.");
333
334 for (i = 0; i < ctx->num_analog_channels + ctx->num_logic_channels; i++) {
335 if (ctx->channels[i].ch->type == SR_CHANNEL_ANALOG) {
336 sr_dbg("Looking for channel %s",
337 ctx->channels[i].ch->name);
338 for (l = meaning->channels, c = 0; l; l = l->next, c++) {
339 struct sr_channel *ch = l->data;
340 sr_dbg("Checking %s", ch->name);
341 if (ctx->channels[i].ch == l->data) {
cad447d2
MM
342 if (ctx->label_do && !ctx->label_names) {
343 sr_analog_unit_to_string(analog,
344 &ctx->channels[i].label);
345 }
9e24c8bc
MM
346 for (j = 0; j < analog->num_samples; j++)
347 ctx->analog_samples[j * ctx->num_analog_channels + i] = fdata[j * num_channels + c];
348 break;
349 }
350 }
351 }
352 }
353 g_free(fdata);
354}
355
356/*
357 * We treat logic packets the same as analog packets, though it's not
358 * strictly required. This allows us to process mixed signals properly.
359 */
360static void process_logic(struct context *ctx,
361 const struct sr_datafeed_logic *logic)
c04cf9aa 362{
9e24c8bc
MM
363 unsigned int i, j, ch, num_samples;
364 int idx;
365 uint8_t *sample;
366
367 num_samples = logic->length / logic->unitsize;
368 ctx->channels_seen += ctx->logic_channel_count;
369 sr_dbg("Logic packet had %d channels", logic->unitsize * 8);
370 if (!ctx->logic_samples) {
371 ctx->logic_samples = g_malloc(num_samples * ctx->num_logic_channels);
372 if (!ctx->num_samples)
373 ctx->num_samples = num_samples;
374 }
375 if (ctx->num_samples != num_samples)
376 sr_warn("Expecting %u samples, got %u",
377 ctx->num_samples, num_samples);
378
379 for (j = ch = 0; ch < ctx->num_logic_channels; j++) {
380 if (ctx->channels[j].ch->type == SR_CHANNEL_LOGIC) {
381 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
382 sample = logic->data + i;
383 idx = ctx->channels[ch].ch->index;
cad447d2 384 if (ctx->label_do && !ctx->label_names)
6a235225 385 ctx->channels[j].label = "logic";
9e24c8bc
MM
386 ctx->logic_samples[i * ctx->num_logic_channels + ch] = sample[idx / 8] & (1 << (idx % 8));
387 }
388 ch++;
389 }
390 }
391}
392
393static void dump_saved_values(struct context *ctx, GString **out)
394{
395 unsigned int i, j, analog_size, num_channels;
396 float *analog_sample, value;
397 uint8_t *logic_sample;
398
399 /* If we haven't seen samples we're expecting, skip them. */
400 if ((ctx->num_analog_channels && !ctx->analog_samples) ||
401 (ctx->num_logic_channels && !ctx->logic_samples)) {
402 sr_warn("Discarding partial packet");
c04cf9aa 403 } else {
9e24c8bc
MM
404 sr_info("Dumping %u samples", ctx->num_samples);
405
c04cf9aa 406 *out = g_string_sized_new(512);
9e24c8bc
MM
407 num_channels =
408 ctx->num_logic_channels + ctx->num_analog_channels;
409
cad447d2 410 if (ctx->label_do) {
9e24c8bc 411 if (ctx->time)
cad447d2
MM
412 g_string_append_printf(*out, "%s%s",
413 ctx->label_names ? "Time" :
414 ctx->xlabel, ctx->value);
9e24c8bc
MM
415 for (i = 0; i < num_channels; i++) {
416 g_string_append_printf(*out, "%s%s",
cad447d2
MM
417 ctx->channels[i].label, ctx->value);
418 if (ctx->channels[i].ch->type == SR_CHANNEL_ANALOG
419 && ctx->label_names)
420 g_free(ctx->channels[i].label);
9e24c8bc
MM
421 }
422 if (ctx->do_trigger)
423 g_string_append_printf(*out, "Trigger%s",
424 ctx->value);
425 /* Drop last separator. */
426 g_string_truncate(*out, (*out)->len - 1);
427 g_string_append(*out, ctx->record);
428
cad447d2 429 ctx->label_do = FALSE;
9e24c8bc
MM
430 }
431
432 analog_size = ctx->num_analog_channels * sizeof(float);
433 if (ctx->dedup && !ctx->previous_sample)
434 ctx->previous_sample = g_malloc0(analog_size + ctx->num_logic_channels);
435
436 for (i = 0; i < ctx->num_samples; i++) {
437 ctx->sample_time += ctx->period;
438 analog_sample =
439 &ctx->analog_samples[i * ctx->num_analog_channels];
440 logic_sample =
441 &ctx->logic_samples[i * ctx->num_logic_channels];
442
443 if (ctx->dedup) {
444 if (i > 0 && i < ctx->num_samples - 1 &&
445 !memcmp(logic_sample, ctx->previous_sample,
446 ctx->num_logic_channels) &&
447 !memcmp(analog_sample,
448 ctx->previous_sample +
449 ctx->num_logic_channels,
450 analog_size))
451 continue;
452 memcpy(ctx->previous_sample, logic_sample,
453 ctx->num_logic_channels);
454 memcpy(ctx->previous_sample
455 + ctx->num_logic_channels,
456 analog_sample, analog_size);
457 }
458
459 if (ctx->time)
8005151b 460 g_string_append_printf(*out, "%" PRIu64 "%s",
9e24c8bc
MM
461 ctx->sample_time, ctx->value);
462
463 for (j = 0; j < num_channels; j++) {
464 if (ctx->channels[j].ch->type == SR_CHANNEL_ANALOG) {
465 value = ctx->analog_samples[i * ctx->num_analog_channels + j];
466 ctx->channels[j].max =
467 fmax(value, ctx->channels[j].max);
468 ctx->channels[j].min =
469 fmin(value, ctx->channels[j].min);
470 g_string_append_printf(*out, "%g%s",
471 value, ctx->value);
472 } else if (ctx->channels[j].ch->type == SR_CHANNEL_LOGIC) {
473 g_string_append_printf(*out, "%c%s",
474 ctx->logic_samples[i * ctx->num_logic_channels + j] ? '1' : '0', ctx->value);
475 } else {
476 sr_warn("Unexpected channel type: %d",
477 ctx->channels[i].ch->type);
478 }
479 }
480
481 if (ctx->do_trigger) {
482 g_string_append_printf(*out, "%d%s",
483 ctx->trigger, ctx->value);
484 ctx->trigger = FALSE;
485 }
486 g_string_truncate(*out, (*out)->len - 1);
487 g_string_append(*out, ctx->record);
488 }
c04cf9aa 489 }
9e24c8bc
MM
490
491 /* Discard all of the working space. */
492 g_free(ctx->previous_sample);
493 g_free(ctx->analog_samples);
494 g_free(ctx->logic_samples);
495 ctx->channels_seen = 0;
496 ctx->num_samples = 0;
497 ctx->previous_sample = NULL;
498 ctx->analog_samples = NULL;
499 ctx->logic_samples = NULL;
c04cf9aa
BG
500}
501
9e24c8bc 502static void save_gnuplot(struct context *ctx)
c04cf9aa 503{
9e24c8bc
MM
504 float offset, max, sum;
505 unsigned int i, num_channels;
506 GString *script;
507
508 script = g_string_sized_new(512);
509 g_string_append_printf(script, "set datafile separator '%s'\n",
510 ctx->value);
cad447d2 511 if (ctx->label_did)
9e24c8bc
MM
512 g_string_append(script, "set key autotitle columnhead\n");
513 if (ctx->xlabel && ctx->time)
514 g_string_append_printf(script, "set xlabel '%s'\n",
515 ctx->xlabel);
516
517 g_string_append(script, "plot ");
518
519 num_channels = ctx->num_analog_channels + ctx->num_logic_channels;
520
521 /* Graph position and scaling. */
522 max = FLT_MIN;
523 sum = 0;
524 for (i = 0; i < num_channels; i++) {
525 ctx->channels[i].max =
526 ctx->channels[i].max - ctx->channels[i].min;
527 max = fmax(max, ctx->channels[i].max);
528 sum += ctx->channels[i].max;
529 }
530 sum = (ctx->scale ? max : sum / num_channels) / 4;
531 offset = sum;
532 for (i = num_channels; i > 0;) {
533 i--;
534 ctx->channels[i].min = offset - ctx->channels[i].min;
535 offset += sum + (ctx->scale ? max : ctx->channels[i].max);
536 }
c04cf9aa 537
9e24c8bc
MM
538 for (i = 0; i < num_channels; i++) {
539 sr_spew("Channel %d, min %g, max %g", i, ctx->channels[i].min,
540 ctx->channels[i].max);
541 g_string_append(script, "ARG1 ");
542 if (ctx->did_header)
543 g_string_append(script, "skip 4 ");
544 g_string_append_printf(script, "using %u:($%u * %g + %g), ",
545 ctx->time, i + 1 + ctx->time, ctx->scale ?
546 max / ctx->channels[i].max : 1, ctx->channels[i].min);
547 offset += 1.1 * (ctx->channels[i].max - ctx->channels[i].min);
c04cf9aa 548 }
9e24c8bc
MM
549 g_string_truncate(script, script->len - 2);
550 g_file_set_contents(ctx->gnuplot, script->str, script->len, NULL);
551 g_string_free(script, TRUE);
c04cf9aa
BG
552}
553
9e24c8bc
MM
554static int receive(const struct sr_output *o,
555 const struct sr_datafeed_packet *packet, GString **out)
02604ed6
UH
556{
557 struct context *ctx;
02604ed6 558
4829d37d
BV
559 *out = NULL;
560 if (!o || !o->sdi)
02604ed6 561 return SR_ERR_ARG;
d686c5ec 562 if (!(ctx = o->priv))
02604ed6 563 return SR_ERR_ARG;
02604ed6 564
9e24c8bc 565 sr_dbg("Got packet of type %d", packet->type);
4829d37d 566 switch (packet->type) {
9e24c8bc
MM
567 case SR_DF_HEADER:
568 *out = gen_header(o, packet->payload);
0f3d8c89 569 break;
9e24c8bc
MM
570 case SR_DF_TRIGGER:
571 ctx->trigger = TRUE;
c04cf9aa 572 break;
4829d37d 573 case SR_DF_LOGIC:
9e24c8bc 574 process_logic(ctx, packet->payload);
02604ed6 575 break;
edb691fc 576 case SR_DF_ANALOG:
9e24c8bc
MM
577 process_analog(ctx, packet->payload);
578 break;
579 case SR_DF_FRAME_BEGIN:
580 *out = g_string_new(ctx->frame);
581 /* And then fall through to... */
582 case SR_DF_END:
583 /* Got to end of frame/session with part of the data. */
584 if (ctx->channels_seen)
585 ctx->channels_seen = ctx->channel_count;
586 if (*ctx->gnuplot)
587 save_gnuplot(ctx);
c04cf9aa 588 break;
02604ed6
UH
589 }
590
9e24c8bc
MM
591 /* If we've got them all, dump the values. */
592 if (ctx->channels_seen >= ctx->channel_count)
593 dump_saved_values(ctx, out);
594
595 return SR_OK;
02604ed6
UH
596}
597
4829d37d 598static int cleanup(struct sr_output *o)
02604ed6
UH
599{
600 struct context *ctx;
02604ed6 601
4829d37d 602 if (!o || !o->sdi)
02604ed6 603 return SR_ERR_ARG;
02604ed6 604
d686c5ec
BV
605 if (o->priv) {
606 ctx = o->priv;
9e24c8bc
MM
607 g_free((gpointer)ctx->record);
608 g_free((gpointer)ctx->frame);
609 g_free((gpointer)ctx->comment);
610 g_free((gpointer)ctx->gnuplot);
611 g_free(ctx->previous_sample);
c04cf9aa 612 g_free(ctx->channels);
d686c5ec
BV
613 g_free(o->priv);
614 o->priv = NULL;
02604ed6
UH
615 }
616
02604ed6
UH
617 return SR_OK;
618}
619
9e24c8bc
MM
620static struct sr_option options[] = {
621 {"gnuplot", "gnuplot", "gnuplot script file name", NULL, NULL},
622 {"scale", "scale", "Scale gnuplot graphs", NULL, NULL},
623 {"value", "Value separator", "Character to print between values", NULL, NULL},
624 {"record", "Record separator", "String to print between records", NULL, NULL},
625 {"frame", "Frame seperator", "String to print between frames", NULL, NULL},
626 {"comment", "Comment start string", "String used at start of comment lines", NULL, NULL},
627 {"header", "Output header", "Output header comment with capture metdata", NULL, NULL},
cad447d2 628 {"label", "Label values", "Type of column labels", NULL, NULL},
9e24c8bc
MM
629 {"time", "Time column", "Output sample time as column 1", NULL, NULL},
630 {"trigger", "Trigger column", "Output trigger indicator as last column ", NULL, NULL},
631 {"dedup", "Dedup rows", "Set to false to output duplicate rows", NULL, NULL},
632 ALL_ZERO
633};
634
635static const struct sr_option *get_options(void)
636{
637 if (!options[0].def) {
638 options[0].def = g_variant_ref_sink(g_variant_new_string(""));
639 options[1].def = g_variant_ref_sink(g_variant_new_boolean(TRUE));
640 options[2].def = g_variant_ref_sink(g_variant_new_string(","));
641 options[3].def = g_variant_ref_sink(g_variant_new_string("\n"));
642 options[4].def = g_variant_ref_sink(g_variant_new_string("\n"));
643 options[5].def = g_variant_ref_sink(g_variant_new_string(";"));
644 options[6].def = g_variant_ref_sink(g_variant_new_boolean(TRUE));
cad447d2 645 options[7].def = g_variant_ref_sink(g_variant_new_string("units"));
9e24c8bc
MM
646 options[8].def = g_variant_ref_sink(g_variant_new_boolean(TRUE));
647 options[9].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
648 options[10].def = g_variant_ref_sink(g_variant_new_boolean(TRUE));
649 }
650
651 return options;
652}
653
a755b0e1 654SR_PRIV struct sr_output_module output_csv = {
02604ed6 655 .id = "csv",
a755b0e1
BV
656 .name = "CSV",
657 .desc = "Comma-separated values",
9e24c8bc 658 .exts = (const char *[]){"csv", NULL},
3cd4b381 659 .flags = 0,
9e24c8bc 660 .options = get_options,
02604ed6 661 .init = init,
4829d37d
BV
662 .receive = receive,
663 .cleanup = cleanup,
02604ed6 664};